Sonar “Close Connection” warning workaround.

Tuesday 2009-10-06

When you use Spring and Ibatis and SQLTemplates, you could have code in your project which looks somewhat like this:

Connection connection = DataSourceUtils.getConnection(getDataSource());
...<do connection stuff here>...
DataSourceUtils.releaseConnection(connection, getDataSource());

Sonar will report that you did not close the connection, while in fact, Spring did that for you. You can not just add a “connection.close()” to the code because the whole point of calling “releaseConnection()” is to have Spring handle all the smart stuff on committing, closing, and returning the connection to the pool if needed.

In our company, not closing the connection is a major blocking violation (and it should be). But in this case, there is no way to make the Jedi wave to Sonar, telling it that “this code will do just fine”. So I added the following trick, albeit a bit dirty:

if (connection.isClosed()){
   // This code is only here to keep Sonar from
   // warning us that the connection is not
   // closed. Please note: Do not just close an
   // unclosed connection, Spring should handle
   // connection closing and returning to the pool.
   connection.close();
}

Use it to your advantage, but use this responsibly. If you see any problems in my solution, or if there is a better way to do this I’d be happy to hear about it.

Edit (2009-12-01):

There is a much better way to do this if you have Spring/Ibatis integration. In stead of DataSourceUtils.getConnection() you can create a new ConnectionCallback object, like so:

 

JdbcTemplate template = new JdbcTemplate(getDataSource());
Object resultObject = template.execute(new ConnectionCallback() {
   public Object doInConnection(Connection conn) 
      throws SQLException, DataAccessException {
   // Do connection stuff here (can return object);
   return null;
});

Please note that this code does NOT contain any Connection.close() references. The connection is passed to you, you can use it, and after your method completes, the framework will do whatever is needed to clean everything up. Sonar will not complain because the whole open/close handeling is done outside your method.

 


Bill Gates works at Apple

Sunday 2009-09-13

Steve JobsI was happy to see Steve return on the Apple keynote of 9 September, because boy does Apple need somebody with clear vision. The introductions on the new iPod line are all over the place. Let me tell you why I think Steve has not been running the operation for the past few months: Read the rest of this entry »


iTunes 9: “Genre-Artist-Album” panes restored

Friday 2009-09-11

iTunes 9 with 3 panesWhat I loved about iTunes is the three pane “Genre-Artist-Album” on top of my music list. It allowed me to quickly go through my music and find that great Anouk album I was looking for. After installing iTunes 9, it was gone. And I was sad. But I found a way to get it back.

Settings to get the panes back

Look at this screenshot (sorry, it’s in Dutch). In iTunes, select “View”, “Show Column Browser”. Now, put it on top by selecting “View”, “Column Browser”, “Top”. Voila, your trusted browser is at the top. You may need to add “Genre” and “Album” there, but you’ve already seen the options for that now, have you? ;-)


Transactions and Isolation levels

Saturday 2009-08-29

Safety googlesAt work, we have two applications which connect to the same database. For all kinds of business reasons, we need to make sure that only one of the applications accesses certain data at the same time. To do this, we use a row in a table as a semaphore.

While working on the locking mechanism, we had a closer look at the Transaction Management and the Isolation Levels we were using. There is a lot of good documentation on Transaction and Isolation, but it tends to be over complete, elaborate and therefore hard to read. I’ll try to share our insights with you in a slightly more digestable form (I hope).

Read the rest of this entry »


Omniplan Progress Tracking Tip

Thursday 2009-08-13

OmniplanDuring one of my Omniplan sessions at work, I discovered that the resource leveling was acting a bit funny, where people were not planned to do any work for days. I played around with a fake planning and soon discovered the problem and several solutions to it.

Read the rest of this entry »