Spring Transactions


Spring can manage transactions for databases and other resources such as JMS connections. Transactions can be demarcated in code or they can be specified declaratively using Java annotations. These notes focus on the declarative approach.

For more details, see the Spring manual chapter on transactions here.

Spring Configuration

Here's an example of a spring configuration file, typically named spring-beans.xml, that configures the use of declarative transactions.

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:jdbc/{database-jndi-name}"/> </bean> <!-- If using iBATIS, this will be needed. --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="/WEB-INF/SqlMapConfig.xml"/> <property name="dataSource" ref="dataSource"/> </bean> <!-- This sets up Spring declarative transactions using @Transactional annotations. --> <tx:annotation-driven transaction-manager="transactionManager"/> <aop:aspectj-autoproxy/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- Classes that use the @Transactional annotation must have their instances loaded using Spring's getBean method. --> <bean id="{bean-name}" class="{package-qualified-class-name}"/> </beans>

Declaring Transactions

Any Java method that needs to run inside a transaction should be preceded by an @Transactional annotation. Use of this annotation is described in section 9.5.6 Using @Transactional in the Spring manual. Properties for specifying non-default transactional behavior can be specified on this annotation. For details on the supported properties, see section @Transactional settings in the Spring manual.

Loading Instances

The following imports are needed in Java source files that load Spring "beans" and use the @Transactional annotation.

import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.transaction.annotation.Transactional;

Typically classes whose instances will be loaded as Spring "beans" implement an interface. Instances are declared in code to have the interface type rather than the class type. This allows loading instances from any class that implements the interface.

The following code is recommended to load a Spring "bean".

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-beans.xml"); {bean-interface} bean = ({bean-interface}) ctx.getBean("{bean-name}");

Copyright © 2007 Object Computing, Inc. All rights reserved.