Spring provides a comprehensive transaction support. And if you are using spring, you must be using transaction management that comes with it. It provides a good set of abstractions for transaction management.
You can implement transaction management with a few configurations in the application context file of spring and no additions or alterations to the java code.
Below is the configuration which is required for single Database transaction management.
<!-- ***** Transaction Manager ****** -->
<bean id="txManager1"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="projectDataSource" />
</bean>
<!-- ****** Transaction Definitions ***** -->
<!-- Default Isolation level is used - READ COMMITTED -->
<tx:advice id="txAdvice1" transaction-manager="txManager1">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allProcessOperations"
expression="execution(* com.company.proj.bo.process.*Process.*(..))" />
<aop:advisor advice-ref="txAdvice1"
pointcut-ref="allProcessOperations" />
</aop:config>
The projectDataSource is ofcource the datasource bean which you must define for your datasource.
The above configuration binds all the operations on the projectDataSource into one transaction.
The AOP pointcut "execution(* com.company.proj.bo.process.*Process.*(..))" indicates that all the methods under all the classes ending with "Process" under the package com.company.proj.bo.process will be considered in transaction management.
The TX and AOP schema locations are
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
Any exception during the execution of a method which is bound under the above transaction will automatically rollback all changes. If the method completes without any exceptions, then the changes will be committed.
Distributed Transaction management
If you have distributed transactions in your method, then we can bind that method under JTA transaction
You may have a scenario where you are altering two data sources and also publishing into a JMS queue. All these operations can be bound under a JTA transaction. The configuration is as below.
<!-- ========== TX MANAGER ============= -->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="javax.transaction.TransactionManager"/>
</bean>
<tx:advice id="txAdviceJTA" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="persistData*" propagation="REQUIRED" timeout="120000"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="processOperations"
expression="execution(* com.company.proj.bo.process.MyProcess.*(..))" />
<aop:advisor advice-ref="txAdviceJTA"
pointcut-ref="processOperations" />
</aop:config>
Note that if a datasource or a JMS connection factory must be used in a JTA transaction, then they must be XA enabled. Otherwise they will not be involved in a JTA transaction.
I missed one point here.
ReplyDeleteWe need to make sure that we are throwing runtime exceptions when we need all the transactions to be rolled back.