Apr 24, 2008

Last Insert Id Without Extra SELECT in Spring

If you have a table with auto_increment for an id, you often need to get this id immediately after a new instance has been created.

The easy way to do this is to perform a SELECT LAST_INSERT_ID() query. However, most of the time you want as less database requests as possible. There is a way to avoid LAST_INSERT_ID() code using Spring's JDBC support.

final String DEF_INSERT_USER_QUERY = "insert into users (username, password) values (?,?)";
final String firstname = createdUser.getFirstname();
final String lastname = createdUser.getLastname();

KeyHolder keyHolder = new GeneratedKeyHolder();
getJdbcTemlpate().update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection)
throws SQLException {
PreparedStatement ps = connection.prepareStatement(DEF_INSERT_USER_QUERY,
new String[] {"firstname", "lastname"});
ps.setString(1, firstname);
ps.setString(2, lastname);
return ps;
}, keyHolder);


Apr 16, 2008

Change J2EE version of NetBeans Project

A java project I've been working was initially started as J2EE1.4 one, but finally had to be moved to J2EE5. Apparently, there's no GUI way to do this in NetBeans (nor 6 neither 5.5).

When you select Project->Properties->Run, J2EE version is just displayed in a text field which value I couldn't change. Maybe this is a bug of NetBeans, maybe this is an intentional limitation.

Anyway, after some painful hours spent in moving classes and configs into a newly created J2EE5 project and fighting errors popped, I just took an advice of a fellow developer, checked how things are done in project.properties file and put j2ee.version=1.5 instead of 1.4.

Apr 7, 2008

Firefox Memory Management Mystery

Firefox's appetite for memory is a well-known problem, especially in the latest 2.0.x versions. However, I never experienced it while I was using it on Windows. The browser never ate more than one and a half hundred megabytes.

I started to suffer from it only when I switched to Ubuntu a couple years ago. In fact, latest version with all the plugins (TMP+Firebug+Webdeveloper+DownThemAll) enabled, consumed up to four hundred megabytes of RAM so I even had to switch to Epiphany to be able to keep other greedy processes running.

But now, on the HP laptop with the same Ubuntu 7.10 the same Firefox acts as it did in the Windows days — one and a half hundred megs no matter how many pages are opened.

I guess, this mystery will never be unriddled.