The packages that need to be imported are:
Cglib-nodep-2.1_3.jar
Com.springsource.org.aspectj.weaver-1.6.8.release.jar
Spring-aspects-3.2.0.release.jar
Spring-aop-3.2.0.release.jar
1. Using annotation based sound parts transaction management
Add the following code to the spring's configuration file:
<bean id= "TransactionManager"
class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource" ></property>
</bean>
<tx:annotation-driven transaction-manager= "TransactionManager"/>
2. Use @transactional marking in service
annotation on an interface
Package com.xinrui.hospital.service;
Import org.springframework.transaction.annotation.Transactional;
/**
* *
@ClassName: Iaccountservice
* @Description: Transfer Service class Interface
* @author Liang Zhicheng
* @date March 22, 2016 afternoon 5 : 26:03
* */
@Transactional public
interface Iaccountservice {
/**
*
* * @Title: Transfer
* @param @param out of account *
@param @param in into account
* @param @param money transfer Amount
* @return Void
*/Public
Boolean transfer (string out, string in, Double);
can also be annotated on a method
Package Com.xinrui.hospital.service.impl;
Import Javax.annotation.Resource;
Import Org.apache.commons.lang.StringUtils;
Import Org.springframework.stereotype.Service;
Import org.springframework.transaction.annotation.Transactional;
Import Com.xinrui.hospital.bean.Account;
Import Com.xinrui.hospital.dao.IAccountDao;
Import Com.xinrui.hospital.service.IAccountService; /** * * @ClassName: Accountserviceimpl * @Description: Transfer Service Implementation class * @author Liang Zhicheng * @date March 22, 2016 afternoon 5:26:36 * * * * * * @Se Rvice (value = "Accountservice") public class Accountserviceimpl implements Iaccountservice {@Resource (name = "Accoun
Tdao ") Private Iaccountdao Accountdao;
/** * * @Title: Transfer * @Description: Transfer * @param @param out Transfer account * @param @param in Transfer account * @param @param money transfer amount * @return void */@Transactional public boolean transfer (string out, string In, Double) {if (stringutils.isnotempty) && stringutils.isnoteMpty (in)) {Account Outaccount = new account ();
Outaccount.setmoney (Money);
Outaccount.setname (out);
Accountdao.outmoney (Outaccount); int i = 1/0;
Exception Location Account Inaccount = new account ();
Inaccount.setmoney (Money);
Inaccount.setname (in);
Accountdao.inmoney (Inaccount);
return false;
}
}
3. Transaction management based on AspectJ XML method
Add the following code in the spring configuration file
<!--Configure transaction notifications: (Transaction Enhancements)-->
<tx:advice id= "Txadvice" transaction-manager= "TransactionManager" >
<tx:attributes>
<!--
Propagation: Propagation behavior of a transaction
isolation : Isolation level of transaction
read-only : Read Only
rollback-for: What happens to the exception rollback
no-rollback-for: Which exceptions occur do not rollback
timeout : Expired information
-->
<TX: Method Name= "Transfer" propagation= "REQUIRED"/>
</tx:attributes>
</tx:advice>
< !--configuration Slice -->
<aop:config>
<!--configuration pointcut-->
<aop:pointcut id= "Pointcut"
expression= "Execution (* com.xinrui.hospital.service.*.* (..))"/>
<!--configuration slice-->
<aop:advisor pointcut-ref= "Pointcut" advice-ref= "Txadvice"/>
</aop:config>
So that the transfer method under the service layer is supported by the transaction.
Summary: Spring divides transaction management into two categories:
1. Programmatic transaction management:
requires manual code for transaction management (rarely used)
2. Declarative Transaction Management:
(1) based on Transactionproxyfactorybean (rarely used)
need to configure a Transactionproxyfactorybean for each transaction managed class
(2) xml-based approach based on ASPECTJ (often used)
once configured, there is no need to configure anything on the class
(3) based on annotations (often used)
configuration is simple, you need to add a @transactional annotation on the business layer class