1.导入 spring-tx-4.3.2.RELEASE.jar
2.开启spring的事务管理有两种声明式方法
- xml声明
<?xml version="1.0" encoding="UTF-8"?>
<!-- 导入tx约束-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--
配置事务管理器,对于不同的dao框架具有不同的事务管理器的实现类
jdbc/MyBatis:org.springframework.jdbc.datasource.DataSourceTransactionManager
hibernate:org.springframework.orm.hibernate3.HibernateTransactionManager
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入连接池(dbcp,c3p0....)-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务管理器具体要增强哪个方法-->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name=“transfarAccounts"/>
</tx:attributes>
</tx:advice >
<!-- 配置切入面和切点(spring实现事务是通过aop的方式实现的) -->
<aop:config>
<aop:pointcut expression="execution(* cn.yellowimg.service.transfarAccounts(..))" id="pointcut1"/>
<aop:advisor advice-ref="advice" pointcut-ref="pointcut1"/>
</aop:config>
</beans>
- 注解声明
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--
配置事务管理器,对于不同的dao框架具有不同的事务管理器的实现类
jdbc/MyBatis:org.springframework.jdbc.datasource.DataSourceTransactionManager
hibernate:org.springframework.orm.hibernate3.HibernateTransactionManager
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入连接池(dbcp,c3p0....)-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 注入连接池(dbcp,c3p0....)-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
在需要开启事务的类上声明注解
//开启事务,该类上的所有方法都会开启事务
@Transactional
public class AccountService {
@Resource(name="accountDao")
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void makemoney()
{
accountDao.edit("小马", 1000);
accountDao.edit("小王", -1000);
}
}