Transaction management under Spring-using AOP XML configuration management (IBatis as an example)
There are three ways to manage things under Spring: programmatic transaction management, declarative transaction management, and AOP transaction management. AOP transaction management is divided into AOP annotation transaction management and AOP XML configuration, which records the following
In the AOP XML configuration management, which is the most recommended way for spring.
Refer to the example of bank transfer in <spring advanced Programming >.
Data source settings for 1.Spring
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" >
< Property Name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/>
<property name= "url" value= "jdbc:mysql:/ /localhost:3306/test "/>
<property name=" username "value=" root "/>
<property name=" Password " Value= "123456"/>
</bean>
2.Spring Support for Ibatis
Spring provides support for Ibatis primarily Org.springframework.orm.ibatis.SqlMapClientFactoryBean classes
<bean id= "sqlmapclient" class= "Org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
<property Name= "DataSource" ref= "DataSource"/>
<property name= "configlocation" value= "/config/sqlmapconfig.xml"/ >
</bean>
3.Spring support for Ibatis DAO
Spring provides org.springframework.orm.ibatis.support.SqlMapClientDaoSupport to support Ibatis DAO, By calling the Getsqlmapclienttemplate () method of the class to obtain a
Ibatis Control access.
<bean id= "Accountdao" class= "com.hj.dao.AccountDaoImp" >
<property name= "sqlmapclient" ref= " Sqlmapclient "/>
<bean id= "Bankservice" class= "Com.hj.bankOps.DefaultBankService" >
<property name= "Accountdao" ref= " Bankaccountdao "/>
Here the Defaultbankservice class primarily implements the Bankservice interface (a method definition that provides the service), which internally references a Bankaccountdao instance to access the database. Bankaccountdao Class Main inheritance
Sqlmapclientdaosupport.
4.Spring Configuration Transactions
<bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource"/>
5.AOP XML Configuration Transaction Management
1). Configure Transaction Notifications
<tx:advice id= "Transactionmanageradivice" transaction-manager= "TransactionManager" >
<tx:attributes >
<tx:method name= "*"
isolation= "read_committed"
propagation= "REQUIRED"
rollback-for= " Java.lang.RuntionException "/>
</tx:attributes>
</tx:advice>
2). Configure Pointcuts and Facets
<aop:config>
<aop:pointcut expression= "Execution (* com.hj.bankops.defaultbankservice.* (..))" Id= " Bankservicepc "/>
<aop:advisor advice-ref=" Transactionmanageradivice "pointcut-ref=" BankServicePc "/>
</aop:config>
Above Execution (* com.hj.bankops.defaultbankservice.* (..)) An expression represents a pointcut as any method in that class. Therefore, transaction management occurs when a method is called in the Defaultbankservice class
, and the rollback operation is automatically performed when the RuntimeException is thrown.
6. Problems encountered
In the <spring Advanced Programming > book, when configuring an AOP XML transaction, its notification section does not have a specific attribute set (missing rollback-for= "Java.lang.RuntionException")
<tx:attributes>
<tx:method name= "*"
isolation= "read_committed"
propagation= "REQUIRED
" />
The transaction does not roll back the corresponding rollback operation if an exception is thrown in the Defaultbankservice method call.
Original link: http://www.iteye.com/topic/614620.