Posts

Showing posts with the label java

Ubuntu 10.04 : Install and set Sun JDK as default

Image
  Since ubuntu 10.04, Sun JDK has been dropped and replaced by openjdk . Literally replaced, ie : Sun JDK remove, openjdk installed. This is performed during the upgrade without explicitly asking you for your consent... which is quite ugly... After the upgrade, my tomcat was not working anymore... Anyway, in order to install Sun JDK back you just have to : vi /etc/apt/sources.list and check if the following line is present : deb http://archive.canonical.com/ lucid partner if not, add it at the end of the file. then run sudo aptitude update to get the package list from the new repo. and then run : sudo aptitude install sun-java6-jdk but after that, if you run java -version, it's still the openjdk version. thomas@daisybox:/usr/lib/jvm/java-6-sun$ java -version java version "1.6.0_18" OpenJDK Runtime Environment (IcedTea6 1.8) (6b18-1.8-0ubuntu1) OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode) thomas@daisybox:/usr/lib/jvm/java-6-sun$ which java /usr/...

Spring Transaction support with JDBCTemplate and Java 1.5 Annotation

Here is quick tutorial to enable transaction in a Spring project using JdbcTemplate and Java 1.5 Annotations. I'm using Spring 2.5.2, Java 1.6. (it should work with Spring 1.2 and Java 1.5 too, considering this blog post, which also gives some usefull info for people new to transaction with Spring ) for example , as simply as this : @Transactional (propagation=Propagation.REQUIRED, rollbackFor=Exception.class) public Intervention createEmptyIntervention(int idRegulation) throws Exception { //...jdbc Code } Let's begin with the beginning, applicationContext.xml : <?xml version="1.0" encoding="UTF-8"?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean class=...

MySQL last_insert_id and Spring Framework JdbcTemplate

I'm using Spring Framework JdbcTemplate on a project with MySQL server as database. And I've noticed that the query "SELECT last_insert_id()" returns 0 most of the time. Using this syntax improves a bit : SELECT last_insert_id() from ` last_inserted_table ` LIMIT 1 The reason is that for each jdbc access, Spring use a connection for it's connection pool. So it may use one connection from its pool, to make the insert, and another connection for the last_insert_id query. The problem is that the last_insert_id query is tied to the connection that actually made the insert. In order to force Spring JdbcTemplate to use the same connection all along a portion of code is to start a Transaction before the insert, and commit it after the last_insert_id query. See this post for simply managing transaction. You might say "I always use transaction"... well, in my case, transaction were not needed until this last_insert_id issue that returned 0.