Spring Framework Declarative Transaction with Annotations
- Using Spring 2.0 and Java 5, it is possible to declare the transactions using annotation-based approach.
- Spring framework supports @Transactional annotation for declarative transaction handling, this annotation can be used on a concrete class or method (with public visibility) of a concrete class. This annotation can be used on an interface, but this will only work as you would expect it to if you are using interface-based proxies.
- The default @Transactional settings are:
- The propagation setting is PROPAGATION_REQUIRED
- The isolation level is ISOLATION_DEFAULT
- The transaction is read/write
- The transaction timeout defaults to the default timeout of the underlying transaction system, or none if timeouts are not supported
- Any RuntimeException will trigger rollback, and any checked Exception will not.
- Enabling the configuration of transactional behavior based on annotations can be done as follows
<tx:annotation-driven transaction-manager=”txManager”/> where txManager points the underlying transaction manager and the tx namespace refers to http://www.springframework.org/schema/tx
- Transaction rollback can be controlled though Transactional annotation for the property ‘rollbackFor’ and ‘rollbackForClassName’.
- Timeout for the transaction can be controlled through the Transactional annotation for the property ‘timeout’.
- Example usage for Declarative transaction with Annotations
<!– the service class that we want to make transactional –>
@Transactional
public class DangerousService implements NightmareService {
Foo getFoo(String fooName){ … }
Foo getFoo(String fooName, String barName) { … }
void insertDangerousObject(DangerousObject foo) {…}
void updateDangerousObject(DangerousObject foo) {…}
}
<!– from the file ’spring 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”
xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:tx=”http://www.springframework.org/schema/tx”
xsi:schemaLocation=”
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd”>
<!– this is the service object that we want to make transactional –>
<bean id=”dangerousService” class=”x.y.service.DangerousService”/>
<!– enable the configuration of transactional behavior based on annotations –>
<tx:annotation-driven transaction-manager=”txManager”/>
<!– a PlatformTransactionManager is still required –>
<bean id=”txManager” class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>
<!– (this dependency is defined somewhere else) –>
<property name=”dataSource” ref=”dataSource”/>
</bean>
<!– other <bean/> definitions here –>
</beans>
Written by Ravi Nallakukkala on April 10th, 2007 with
no comments.
Read more articles on Spring.
- [+] Digg: Feature this article
- [+] Del.icio.us: Bookmark this article
- [+] Furl: Bookmark this article