版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ccnuacmhdu/article/details/85019618
概念
事务无非是打开、回滚、提交,所以提供了统一的接口。spring提供操作事务的统一接口PlatformTransactionManager,不同平台提供各自的实现类,如JDBCTransactionManager,HibernateTransactionManager。
事务传播行为
不同的业务方法互相调用的时候,比如业务A调用业务B,怎么处理事务?
有7种处理方式,一般处理事务的方式(REQUIRED)是如果A开启了事务就用A的事务,如果A没开启事务,就新开启一个事务。
银行转账问题
不使用事务
package cn.ccnuacmhdu.dao;
public interface AccountDao {
public abstract void addMoney(Integer id, Double money);//增加钱
public abstract void reduceMoney(Integer id, Double money);//减少钱
}
package cn.ccnuacmhdu.dao;
import org.springframework.jdbc.core.JdbcTemplate;
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate jTemplate;
public void addMoney(Integer id, Double money) {
String sql = "update account set money = money + ? where id = ?";
jTemplate.update(sql, money, id);
}
public void reduceMoney(Integer id, Double money) {
String sql = "update account set money = money - ? where id = ?";
jTemplate.update(sql, money, id);
}
public JdbcTemplate getjTemplate() {
return jTemplate;
}
public void setjTemplate(JdbcTemplate jTemplate) {
this.jTemplate = jTemplate;
}
}
package cn.ccnuacmhdu.service;
public interface AccountService {
//转账
public abstract void transfer(Integer from, Integer to, Double money);
}
package cn.ccnuacmhdu.service;
import cn.ccnuacmhdu.dao.AccountDao;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void transfer(Integer from, Integer to, Double money) {
accountDao.reduceMoney(from, money);
int x = 1/0;
accountDao.addMoney(to, money);
}
public AccountDao getAccountDao() {
return accountDao;
}
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
}
依赖关系分析:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<!-- 指定spring读取db.properties配置 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 1.把连接池注入到spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 2.把JDBCTemplate注入到spring容器 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3.把AccountDaoImpl注入到spring容器 -->
<bean name="AccountDaoImpl" class="cn.ccnuacmhdu.dao.AccountDaoImpl">
<property name="jTemplate" ref="jdbcTemplate"></property>
</bean>
<!-- 4.把AccountServiceImpl注入到spring容器 -->
<bean name="AccountServiceImpl" class="cn.ccnuacmhdu.service.AccountServiceImpl">
<property name="accountDao" ref="AccountDaoImpl"></property>
</bean>
</beans>
测试:
package cn.ccnuacmhdu.transaction;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.ccnuacmhdu.service.AccountService;
public class Demo {
@Test
public void fun1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) applicationContext.getBean("AccountServiceImpl");
accountService.transfer(1, 2, 100d);
}
}
可以实现正常转账,但是如果制造一个错误如下:
这会导致中途转账失败,账户from凭空少了money,而to钱不变。
使用事务(Transaction模板编码方式)
略
(springAOP)使用事务( XML配置)
springAOP管理事务,spring已经提供了事务通知,目标对象是AccountService,只需要把通知织入到目标对象的切点即可。
导包:
引入名字空间:
上面代码不变,进行如下XML文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 指定spring读取db.properties配置 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 配置事务通知 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 以方法为单位,指定方法应用什么事务属性
isolation:事务隔离级别
propagation:事务传播行为
read-only:是否只读
-->
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<!-- 批量配置如下 -->
<!-- <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /> -->
</tx:attributes>
</tx:advice>
<!-- 配置织入 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* cn.ccnuacmhdu.service.*ServiceImpl.*(..))" id="pointCut"/>
<!-- 配置切面:通知+切点
advice-ref:通知
pointCut:切点
-->
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="pointCut"/>
</aop:config>
<!-- 1.把连接池注入到spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 2.把JDBCTemplate注入到spring容器 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3.把AccountDaoImpl注入到spring容器 -->
<bean name="AccountDaoImpl" class="cn.ccnuacmhdu.dao.AccountDaoImpl">
<property name="jTemplate" ref="jdbcTemplate"></property>
</bean>
<!-- 4.把AccountServiceImpl注入到spring容器 -->
<bean name="AccountServiceImpl" class="cn.ccnuacmhdu.service.AccountServiceImpl">
<property name="accountDao" ref="AccountDaoImpl"></property>
</bean>
</beans>
再次进行测试,运行没问题。再次造错误,事务会自动回滚,不会让人凭空消失钱。
(springAOP)使用事务(注解配置)
配置文件中开启注解管理事务
<!-- 开启使用注解管理aop事务 -->
<tx:annotation-driven/>
在目标对象类或对于方法上使用注解: