• Search and lock in a single query.
    Feb 11, 2010

    There is a task in the current project I am working with, which is getting a product (that could be anything, in our case it was products users had to do some work with) for a user logged in to the system, and simultaneously locking it so that no other user can access it. While my first idea about solution was having a synchronized piece of code which would first make a query to search for a product, and another query to lock it in the database. Note that we are using database table for locking.

    But thinking of it a bit more, I thought that why not to do this in a single query, if this is possible. There will be no need for synchronized code in that case, because mysql queries for InnoDB storage engine are ACID compliant. So now instead of first getting the product and then locking it, I will try to reverse the order. First search for a product using our search/suggest algorithm and lock it in a single query, and then retrieve it to send it to a user. If it did not exist before, you will also need to add the feature of being able to determine to whom the lock from the object belongs to. In stubs it would look similar to this:

    Old way :

    select * from product where... /*complex query for product suggest/search*/;
    update lock where product and user;

    New way:

    update lock where lock and product and product =/*complex query for product suggestsearch*/;
    select product where product is locked and lock = user

    Maybe there is even better and cleaner solution with mysql routines, but those are still on my "To Study" list!

  • MySQL update bug in Ubuntu, MySQL clean install with --purge parameter to apt-get
    Feb 11, 2010

    Today while updating my Ubuntu installation(Ubuntu 9.10 Karmic Koala) , mysql package also had an update, but after installation it refused to start. Version of package failing to install was mysql-server 5.1.37-1ubuntu5. Looking around I found out that problems with mysql updates occur quite frequently, and what usually helps is a fresh install.

    So here is what I did after finding a solution in a thread in launchpad forum (reply #13)

    sudo apt-get remove --purge mysql-server-5.1
    sudo apt-get autoremove
    sudo rm -rf /etc/mysql
    sudo apt-get install mysql-server-5.1

    Before removing /etc/mysql I backed it up in my folder, just in case. But it was not necessary, after these operations everything worked like a charm! Even my databases from before were there.I have also heard from other people that there is a problem of making a fresh install of MySQL in Ubuntu, because old configurations and parameters remain. I believe the key point they missed trying to do that was --purge parameter for apt-get.

  • java.util.Arrays.asList(T... a) method returns an immutable ArrayList!
    Feb 10, 2010

    It does, to my biggest surprise, I got UnsupporttedOperationException when I tried to add objects into the list I received via invoking a call to Arrays.asList(). And even bigger surprise that looking at the stack trace I found out that the exception is thrown from the add() method of java.util.AbstractList.

    Surprised by this, I wanted to see what is the runtime class of the list I am trying to add things into, because AbstractList is impossible to initialize, because it is abstract, and most of List implementations are overriding add() method. Note that at the time of having this exception, I did not know that the list I am working with is the return value of Arrays.asList(). Printing the class of the list, I got java.util.Arrays$ArrayList.

    This meant that there was another ArrayList class different from java.util.ArrayList inside Arrays. Looking to the source code I found that in fact it does, and its a private class of Arrays, instance of which is returned as a value for asList() method. And that ArrayList extends java.util.AbstractList and does not override add() and remove() methods!

    The way to get around this is to wrap what asList() method returns into an ArrayList :

    List<E> list = new ArrayList<E>(Arrays.asList(...));
  • Enhanced (or not so) for loop and ConcurrentModificationException while iterating over the List
    Feb 9, 2010

    Today I was writing a code that would loop over an ArrayList and if some condition occurs remove it from the collection. It looked similar to this:

    ArrayList<Apple> apples = new ArrayList<Apple>();
    for (Apple a : apples) {    
        if (a.isRotten()) apples.remove(a);

    When I first ran this code, it threw a ConcurrentModificationException, which surprised me at that point, because I did not see anything suspicious in the code. Well, Google is a friend in such cases, and doing a bit of searching I found that it is not recommended to modify a collection while iterating over it with for-each loop. This means you should not delete or add elements to the collection inside the for-each loop. You are free to modify the internals objects in the collection though.

    Learning this I got interested what is the reason of such behavior. Turns out the iterator which is used inside enhanced for loop (reminder: enhanced for loop is internally converted to a iteration loop) has a checkForModification() method, which checks for two variables, modCount and expectedModCount to be the same, and if they are not, it throws a ConcurrentModificationException.Essentially Iterator is a stateful object, and when clash with the state of collections occurs,this exception is thrown.The following two stubs work perfectly:

    ArrayList<Apple> apples = new ArrayList<Apple>();
    for (Iterator<String> it = apples.iterator(); it.hasNext(); ){
        if(it.next().isRotten()) it.remove();
    ArrayList<Apple> apples = new ArrayList<Apple>();
    for (int i = 0; i < apples.size(); i++){
        if(apples.get(i).isRotten()) apples.remove(apples.get(i));

    The reason for first piece of code to work is that we invoke remove() method of Iterator, not ArrayList, and doing this does not desynchronize Iterator and ArrayList. And second one works because we just get the size and store it as a separate variable.

  • Java identifier names and currency character(s).
    Feb 7, 2010

    During studying for SCJP I came to the topic of valid characters for identifiers, and what caught my attention was that its not only $ character is meant as currency character, but also €, £ and probably all other currency signs. So valid identifiers are characters, numbers, underscore and currency characters. And also we must not forget that number cannot be the first character of the name of an identifier.

    class Test {
      public static void main(String args[]){
        int uro = 4;

    This compiles and runs perfectly. Another issue is how suitable is the usage of these characters as variable names, but you never know what a programmer's mind can come up with :)

  • Servlet loading on start-up
    Jan 13, 2010

    I recently had a task where I had to make a servlet which would store a configuration data for a large GWT project. Writing a servlet that would read everything from web.xml file, I later realized that servlet needs to be initialized before it has values from web.xml, and Tomcat initializes it only when the request is made towards it. Looking through various solutions, I found out that the easiest way would be telling Tomcat to initialize it on load, or on startup, rather than initializing it on first request. It can be achieved by simply adding <load-on-startup>1</load-on-startup> between <servlet> tags in web.xml file. Overall picture looks something like this:


    Number also tells you in which order to initialize the servlet, so if you have 2 servlets and you want to initialize one after each other, in first servlet you have to put <load-on-startup>1</load-on-startup> and in second one <load-on-startup>2</load-on-startup>

  • Focusing on elements in GWT 1.7 with setFocus()
    Dec 8, 2009

    I found today that doing setFocus on elements may not work properly all the time, due to thread issue. Solution to this problem would be using DeferredCommand class with passing a new Command to it. So instead of


    we need to write

    DeferredCommand.addCommand(new Command() {
        public void execute() {

    This works with GWT 1.7


subscribe via RSS