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
Written by Ravi Nallakukkala on April 10th, 2007 with no comments.
Read more articles on Spring.