Spring的事务管理
什么是事务
事务:逻辑上的一组操作,组成这组操作的各个单元,要么全都成功,要么全都失败。
事务的特性
原子性:事务不可分割
一致性:事务执行前后数据完整性保持一致
隔离性:一个事务的执行不应该受到其他事务的干扰
持久性:一旦事务结束,数据就持久化到数据库
如果不考虑隔离性引发安全性问题
读问题
脏读 :一个事务读到另一个事务未提交的数据
不可重复读 :一个事务读到另一个事务已经提交的update的数据,导致一个事务中多次查询结果不一致
虚读、幻读 :一个事务读到另一个事务已经提交的insert的数据,导致一个事务中多次查询结果不一致。
写问题
丢失更新
解决读问题
设置事务的隔离级别
Serializable :解决所有读问题。
Repeatable read :重复读,解决脏读和不可重复读,但是虚读有可能发生。
Read committed :已提交读,解决脏读,但是不可重复读和虚读有可能发生。
Read uncommitted :未提交读,任何读问题解决不了。
Spring的事务管理的API
PlatformTransactionManager:平台事务管理器
平台事务管理器:接口,是Spring用于管理事务的真正的对象。
DataSourceTransactionManager :底层使用JDBC管理事务
HibernateTransactionManager :底层使用Hibernate管理事务
TransactionDefinition :事务定义信息
事务定义:用于定义事务的相关的信息,隔离级别、超时信息、传播行为、是否只读
TransactionStatus:事务的状态
事务状态:用于记录在事务管理过程中,事务的状态的对象
事务管理的API的关系
Spring进行事务管理的时候,首先平台事务管理器根据事务定义信息进行事务的管理,在事务管理过程中,产生各种状态,将这些状态的信息记录到事务状态的对象中
Spring的事务的传播行为
Spring的传播行为
Spring中提供了七种事务的传播行为:
保证多个操作在同一个事务中
PROPAGATION_MANDATORY :如果A中有事务,使用A中的事务。如果A没有事务,抛出异常。
PROPAGATION_SUPPORTS :支持事务,如果A中有事务,使用A中的事务。如果A没有事务,不使用事务。
PROPAGATION_REQUIRED :默认值,如果A中有事务,使用A中的事务,如果A没有,创建一个新的事务,将操作包含进来
保证多个操作不在同一个事务中
PROPAGATION_NEVER :如果A中有事务,报异常。
PROPAGATION_NOT_SUPPORTED :如果A中有事务,将A的事务挂起。不使用事务管理。 PROPAGATION_REQUIRES_NEW :如果A中有事务,将A的事务挂起(暂停),创建新事务,只包含自身操作。如果A中没有事务,创建一个新事务,包含自身操作
嵌套式事务
PROPAGATION_NESTED :嵌套事务,如果A中有事务,按照A的事务执行,执行完成后,设置一个保存点,执行B中的操作,如果没有异常,执行通过,如果有异常,可以选择回滚到最初始位置,也可以回滚到保存点。
Spring的事务管理
搭建Spring的事务管理的环境
创建Service的接口和实现类
/** * 转账的业务层的接口 */ public interface AccountService { public void transfer(String from,String to,Double money); }
/** * 转账的业务层的实现类 */ public class AccountServiceImpl implements AccountService { // 注入DAO: private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override /** * from:转出账号 * to:转入账号 * money:转账金额 */ public void transfer(final String from, final String to, final Double money) { accountDao.outMoney(from, money); accountDao.inMoney(to, money); } }
创建DAO的接口和实现类
/** * 转账的DAO的接口 */ public interface AccountDao { public void outMoney(String from ,Double money); public void inMoney(String to ,Double money); }
/** * 转账的DAO的实现类 */ public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { @Override public void outMoney(String from, Double money) { this.getJdbcTemplate().update("update account set money = money - ? where name = ?", money,from); } @Override public void inMoney(String to, Double money) { this.getJdbcTemplate().update("update account set money = money + ? where name = ?", money ,to); } }
配置Service和DAO:交给Spring管理
<!-- 配置Service============= --> <bean id="accountService" class="com.itheima.tx.demo1.AccountServiceImpl"> <!-- 给service注入dao--> <property name="accountDao" ref="accountDao"/> </bean>
<!-- 配置DAO================= --> <bean id="accountDao" class="com.itheima.tx.demo1.AccountDaoImpl"> <!-- 给Dao住如dataSource--> <property name="dataSource" ref="dataSource"/> </bean>
在DAO中编写扣钱和加钱方法:
配置连接池和JDBC的模板
<!-- 配置连接池和JDBC的模板 --> <!-- 第二种方式通过context标签引入的 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置C3P0连接池=============================== --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>
在DAO注入Jdbc的模板
/** * 转账的DAO的实现类 * 继承JdbcDaoSupport 内部的方法只要我们注入连接池(配置文件中的dataSource), * 就可以生成一个jdbc模板,可以减少我们需要配置的东西(不需要配置jdbc模板) */ public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { @Override public void outMoney(String from, Double money) { this.getJdbcTemplate().update("update account set money = money - ? where name = ?", money,from); } @Override public void inMoney(String to, Double money) { this.getJdbcTemplate().update("update account set money = money + ? where name = ?", money ,to); } }
Spring的事务管理:一类:编程式事务(需要手动编写代码)--了解
第一步:配置平台事务管理器
<!-- 配置平台事务管理器============================= -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
第二步:Spring提供了事务管理的模板类
配置事务的管理的模板类
<!-- 配置事务管理的模板 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
</bean>
第三步:在业务层注入事务管理的模板
<!-- 配置Service============= -->
<bean id="accountService" class="com.itheima.tx.demo1.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
<!-- 注入 事务管理的模板 -->
<property name="trsactionTemplate" ref="transactionTemplate"/>
</bean>
编写事务管理的代码
/**
* 转账的业务层的实现类
*/
public class AccountServiceImpl implements AccountService {
// 注入DAO:
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
// 注入事务管理的模板
private TransactionTemplate trsactionTemplate;
public void setTrsactionTemplate(TransactionTemplate trsactionTemplate) {
this.trsactionTemplate = trsactionTemplate;
}
@Override
/**
* from:转出账号
* to:转入账号
* money:转账金额
*/
public void transfer(final String from, final String to, final Double money) {
trsactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
accountDao.outMoney(from, money);
int d = 1/0;
accountDao.inMoney(to, money);
}
});
}
}
Spring的事务管理:二类:声明式事务管理(通过配置实现)---AOP
XML方式的声明式事务管理
第一步:引入aop的开发包
第二步:恢复转账环境
第三步:配置事务管理器
<!-- 配置事务管理器=============================== -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
第四步:配置增强
<!-- 配置事务的增强=============================== 相当于一个切面-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 事务管理的规则 -->
<!-- <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/> -->
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
第五步:AOP的配置
将上面的事务通知,添加到目标方法上去
<!-- aop的配置 --> <aop:config> <aop:pointcut expression="execution(* com.itheima.tx.demo2.AccountServiceImpl.*(..))" id="pointcut1"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/> </aop:config>
业务层代码
/** * 转账的业务层的实现类 */ public class AccountServiceImpl implements AccountService { // 注入DAO: private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override /** * from:转出账号 * to:转入账号 * money:转账金额 */ public void transfer( String from, String to, Double money) { accountDao.outMoney(from, money); // int d = 1/0; accountDao.inMoney(to, money); }
测试
/** * 测试转账的环境 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:tx2.xml") public class SpringDemo1 { @Resource(name="accountService") private AccountService accountService; @Test public void demo1(){ accountService.transfer("赵冠希", "何菊花", 1000d); } }
注解方式的声明式事务管理
第一步:引入aop的开发包
第二步:恢复转账环境
第三步:配置事务管理器
<!-- 配置事务管理器=============================== -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
第四步:开启注解事务
<!-- 开启注解事务================================ -->
<tx:annotation-driven transaction-manager="transactionManager"/>
第五步:在业务层添加注解
/**
* 转账的业务层的实现类
*/
@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED)
public class AccountServiceImpl implements AccountService {
// 注入DAO:
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
/**
* from:转出账号
* to:转入账号
* money:转账金额
*/
public void transfer( String from, String to, Double money) {
accountDao.outMoney(from, money);
int d = 1/0;
accountDao.inMoney(to, money);
}
}