Spring Framework Transaction Management

In our previous post we linked up a JDBC connection defined in our Tomcat context.xml file to the Spring Framework. The way the datasource was defined in Spring was as a bean.

<bean id="dataSource">
<property name="jndiName"><value>java:comp/env/jdbc/test</value></property>
</bean>

In order to enable Transaction Management for our dataSource is to wrap it with Spring’s Transaction Manager bean.

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource"><ref local="dataSource" /></property>
</bean>

Next, one will have to reference this Transaction Manager wrapped dataSource in a Manager bean of one’s choosing that holds the JDBC SQL for your application.

For example, one should have an interface and then an implementation that does the actual JDBC work.

// the service interface that we want to make transactional
package foo.bar.dao;

public interface FooJdbcInterface {

Foo getFoo(String fooName);

void updateFoo(long fooId, String fooString);

}

————————————————————————————–
// an implementation of the above interface

package foo.bar.dao;

public class FooJdbcImpl implements FooJdbcInterface {

public Foo getFoo(String fooName) {
String someString = (String) getJdbcTemplate().queryForObject("select somestring from sometable where somecolumn = ?", new Object[]{fooName}, String.class);
}

public void updateFoo(long fooId, String fooString) {

try{
// update some column
getJdbcTemplate().update("update sometable set somecolumn = 'bar' where anothercolumn = ?", new Object[]{fooId});

// do some other query, yet if there is no result that comes back, there will be an Exception
String anotherString = (String) getJdbcTemplate().queryForObject("select somestring from sometable where somecolumn = ?", new Object[]{fooString}, String.class);

// do another update
getJdbcTemplate().update("update anothertable set somecolumn = 'foo' where anothercolumn = ?", new Object[]{fooId});
}
catch(Exception e){
log.error("there has been an error, rolling back...");
}

}

}

So, now we have a couple of methods in our JDBC class that will perform some JDBC operations (via Spring JdbcTemplate), when called by any what who Controller. The method that we are going to focus on is the updateFoo method. Basically, we want to ensure that all of the JDBC operations are performed as a whole and not on a 1 by 1 basis. For example, say our first update goes through “update sometable set somecolumn = ‘bar’ where anothercolumn = ?”, then there is a query “select somestring from sometable where somecolumn = ?” to get a value for the next update “update anothertable set somecolumn = ‘foo’ where anothercolumn = ?”.

What if the query to get the value for the second update fails? We then have a situation where only half of the updateFoo methods operations have succeeded and because of that there could be some down the line data issues that arise due to 1 update being committed to the database and the 2nd update not even being executed due to the fact that the select query failed to bring any results and an Exception was thrown.

Transaction Management will ensure that the updateFoo method will execute as an all or nothing operation. If the select query fails to bring any results, then the first update of the updateFoo method will be rolled back; therefore the data integrity of the database schema will be preserved.

To do this, one must use some AOP to tell Spring Transaction Management what to observe.

First, hook up the required Spring namespaces to your xml configuration file so that we can use the special tags we need to get Transaction Management monitoring working for our app.

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

Now, that we have registered our tx and aop namespaces, let’s put those tags to work. Use the below tx tag configuration to tell our Spring Transaction Management to observe and monitor all “update” methods for the particular interface we want Transaction Management to work for.

<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="update*" propagation="REQUIRED" isolation="READ_COMMITTED" rollback-for="Exception" />
</tx:attributes>
</tx:advice>

Next, use AOP pointcuts to tell Spring Transaction Management which package and interface to watch.

<!-- ensure that the above transactional advice runs for any execution
of an operation defined by the FooJdbcInterface interface -->
<aop:config>
<aop:pointcut id="fooOperation" expression="execution(* foo.bar.dao.FooJdbcInterface.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooOperation"/>
</aop:config>

Now, any update methods of a class that implements foo.bar.dao.FooJdbcInterface will officially be using Spring Transaction Management.

There are also a couple extra jars that one needs besides the regular Spring entourage of jars to get Transaction Management to work. Basically, some libraries to get AOP rolling within your app.

aspectj.runtime-1.6.8.RELEASE.jar
aspectj.weaver-1.6.8.RELEASE.jar

With these configurations, one should be up and running with Spring Transaction Management. Make sure and use your DEBUG logging settings to see what Spring is doing in the background when you need to trouble shoot and make sure that Transaction Management is working for your app.

Cheers, Javaclaus

Advertisements

About javaclaus

Java Programmer, Code master, mountain biker, snowboarder, etc.
This entry was posted in Spring Framework. Bookmark the permalink.

One Response to Spring Framework Transaction Management

  1. Rahul Jha says:

    precise and informative..thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s