Recently I get an EmptyResultDataAccessException in a project where we use Spring JDBC. The full exception listing is "org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0". When I trace the exception I see that I'm calling queryForLong() method of JdbcTemplate for "SELECT Column FROM TABLENAME" on an empty table. Column here is of Long type. Now, I plan to catch the exception and return an unusual value (like -1) as a result. On the service layer, I plan to check for equality to the unusual value and proceed with the right logical flow.
Notice that if you use some function like count(), min(), max() ... in the same query (SELECT MAX(Column) FROM TABLENAME) you won't get the same exception but a "0" result instead.
Monday, May 30, 2011
Wednesday, May 11, 2011
a great way of improving your java code: findbugs!
We decided to make our project's code more robust and bug-free. I suggested the use of Findbugs which is basically a program that uses static analysis to search for bugs in your java code. It can be used as IDE plug-in or as a stand-alone application. Findbugs pinpoints problematic places in your code. There are many type of pre-defined problems that Findbugs looks for like duplicate case branches in switch statements, streams which are not closed in all cases, inefficient use of map iterator, redundant comparison to null ... etc. I can't recommend Findbugs enough.
Tuesday, February 22, 2011
bufferedwriter or windows, which one of you is my enemy?
I was planning to name a file I generated using the username and the date of generation, so my naming format was like "01-03-2010-13:33-user1". When I'm trying to do that with BufferedWriter of our beloved Java, this file is not created. ":" is not allowed by Windows so a file with "01-03-2010-13" as a name is created and nothing is inserted although I called bufferedWriter.write("something"). The good part is that no exception is thrown by BufferedWriter. Great isnt it?
Monday, February 7, 2011
thank you axis2 for this handful of close_wait
In one of our projects we are using axis2 1.4 for generating the client side of the web service. Our web service part is working for few hours but gets stuck for no apparent reason. After I investigate a little I saw that it generates lots of sockets with CLOSE_WAIT state which results this freeze. I google a bit about similar problems with Axis2 and saw that this a known bug. Here's the source I used for fixing it. As explicitly calling the garbage collector didn't seem like a good solution, and as I can't upgrade axis2, I opt for changing the HTTP version to 1.0. For this I set a single field in the ServiceStub by calling
from ServiceStub where this object is created.
After that, this CLOSE_WAIT bug is fixed.
serviceStub._getServiceClient().getOptions()
.setProperty(
org.apache.axis2.transport.http.HTTPConstants.HTTP_PROTOCOL_VERSION,
org.apache.axis2.transport.http.HTTPConstants.HEADER_PROTOCOL_10)
from ServiceStub where this object is created.
After that, this CLOSE_WAIT bug is fixed.
Thursday, February 3, 2011
no such method? you must be joking java.lang.NoSuchMethodError
In our project we use Spring Security's new release and we didn't care about the versions of different Spring components like Spring Beans, Core etc and probably left few old versions next to new versions. The result is a nice exception and I had to figure out why.
After a little bit of googling I saw that the root cause is the fact that we use old versions in some Spring components next to new ones. I removed the old ones from pom.xml and everything's fixed.
java.lang.NoSuchMethodError: org.springframework.beans.MutablePropertyValues.add(Ljava/lang/String;Ljava/lang/Object;)Lorg/springframework/beans/MutablePropertyValues;
After a little bit of googling I saw that the root cause is the fact that we use old versions in some Spring components next to new ones. I removed the old ones from pom.xml and everything's fixed.
Subscribe to:
Posts (Atom)