Spring Transaction Management
Global transactions
- JTA UserTransaction normally needs to be sourced from JNDI – JTA is also only available in an application server environment
- Previously, the preferred way to use global transaction was via EJB CMT
- CMT is a form of Declarative Transaction Management
- CMT removes the need for JNDI look up but EJB needs JNDI
- CMT needs JTA which means it needs an application server which are its main drawback
- Works across multiple resources through application server (container managed transaction)
Local transaction
- Resource specific, such as JDBC
- Does not work across multiple resources
- Invasive to programming model
How to create transactional advice using XML
1. Select a transaction strategy by selecting a PlatformTransactionManager. The manager normally has knowledge of the environment in which they work: JDBC, JTA, Hibernate, etc.. The manager would be a class derived from AbstractPlatformTransactionManager.
<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManage">
<propertyname="dataSource"ref="FITS_db"/>
</bean>
All exceptions thrown by the PlatformTransactionManager interface are unchecked.
2. Or use the JTA container-managed transaction
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
3. Create class that needs to be made transactional. Call PlatformTransactionManager to create and manage transaction. This can be done outside of class or done inside of class as part of method behavior.
4. Use AOP to create configuration and advice. Spring would automatically create proxies for eligible beans in IoC container. Beware that inherited methods are not covered and that a derived class that has no method is not proxied. Basically, Spring can only proxy public methods.
5. Or use annotation, for instance,
@Transactional(rollbackFor = Throwable.class)
With annotation no advice elements are needed in XML. All that’s required is the AbstractPlatformTransactionManager bean and the transaction class bean.