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 :
Maybe there is even better and cleaner solution with
mysql routines, but those are still on my "To Study" list!
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
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)
/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
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
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
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
java.util.AbstractList and does not override
The way to get around this is to wrap what
asList() method returns into an
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:
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:
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.Read more...
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.
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 :)Read more...
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
<servlet> tags in
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
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
This works with GWT 1.7Read more...
subscribe via RSS