Spring——事务
6.1 数据库事务并发问题
假设现在有两个事务:Transaction01和Transaction02并发执行。
①脏读
[1]Transaction01将某条记录的AGE值从20修改为30。
[2]Transaction02读取了Transaction01更新后的值:30。
[3]Transaction01回滚,AGE值恢复到了20。
[4]Transaction02读取到的30就是一个无效的值。
②不可重复读
[1]Transaction01读取了AGE值为20。
[2]Transaction02将AGE值修改为30。
[3]Transaction01再次读取AGE值为30,和第一次读取不一致。
③幻读
[1]Transaction01读取了STUDENT表中的一部分数据。
[2]Transaction02向STUDENT表中插入了新的行。
[3]Transaction01读取了STUDENT表时,多出了一些行。
6.2 隔离级别
数据库系统必须具有隔离并发运行各个事务的能力,使它们不会相互影响,避免各种并发问题。一个事务与其他事务隔离的程度称为隔离级别。SQL标准中规定了多种事务隔离级别,不同隔离级别对应不同的干扰程度,隔离级别越高,数据一致性就越好,但并发性越弱。
①读未提交:READ UNCOMMITTED
允许Transaction01读取Transaction02未提交的修改。
②读已提交:READ COMMITTED
要求Transaction01只能读取Transaction02已提交的修改。
③可重复读:REPEATABLE READ
确保Transaction01可以多次从一个字段中读取到相同的值,即Transaction01执行期间禁止其它事务对这个字段进行更新。
④串行化:SERIALIZABLE
确保Transaction01可以多次从一个表中读取到相同的行,在Transaction01执行期间,禁止其它事务对这个表进行添加、更新、删除操作。可以避免任何并发问题,但性能十分低下。
⑤各个隔离级别解决并发问题的能力见下表
⑥各种数据库产品对事务隔离级别的支持程度
tx.xml(注解方式)
<?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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<context:component-scan base-package="com.atguigu"></context:component-scan>
<!-- 0、引入外部配置文件 -->
<context:property-placeholder location="classpath:dbconfig.properties" />
<!-- 1、配置数据源 -->
<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
</bean>
<!-- 2、配置JdbcTemplate操作数据库 value="#{pooledDataSource}" ref="pooledDataSource"-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" value="#{pooledDataSource}"></property>
</bean>
<!-- 3、配置声明式事务
1)、Spring中提供事务管理器(事务切面),配置这个事务管理器
2)、开启基于注解的事务式事务;依赖tx名称空间
3)、给事务方法加注解
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="pooledDataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
BookService.java
@Service
public class BookService {
@Autowired
BookDao bookDao;
// @Autowired
// BookService bookService;
/**
* 事务细节:
* isolation-Isolation:事务的隔离级别;
*
*
*
* noRollbackFor-Class[]:哪些异常事务可以不回滚
* noRollbackForClassName-String[](String全类名):
*
* rollbackFor-Class[]:哪些异常事务需要回滚;
* rollbackForClassName-String[]:
*
* 异常分类:
* 运行时异常(非检查异常):可以不用处理;默认都回滚;
* 编译时异常(检查异常):要么try-catch,要么在方法上声明throws
* 默认不回滚;
*
* 事务的回滚:默认发生运行时异常都 回滚,发生编译时异常不会回滚;
* noRollbackFor:哪些异常事务可以不回滚;(可以让原来默认回滚的异常给他不回滚)
* noRollbackFor={ArithmeticException.class,NullPointerException.class}
* noRollbackForClassName
*
* rollbackFor:原本不回滚(原本编译时异常是不回滚的)的异常指定让其回滚;
*
* readOnly-boolean:设置事务为只读事务:
* 可以进行事务优化;
* readOnly=true:加快查询速度;不用管事务那一堆操作了。
*
* timeout-int(秒为单位):超时:事务超出指定执行时长后自动终止并回滚
* @throws FileNotFoundException
*
*
* propagation-Propagation:事务的传播行为;
* 传播行为(事务的传播+事务的行为);
* 如果有多个事务进行嵌套运行,子事务是否要和大事务共用一个事务;
* 传播行为:
* AService{
* tx_a(){
* //a的一些方法
* tx_b(){
* }
* tx_c(){
* }
* }
* }
*
*
*/
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void checkout(String username,String isbn){
//1、减库存
bookDao.updateStock(isbn);
int price = bookDao.getPrice(isbn);
// try {
// Thread.sleep(3000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
//2、减余额
bookDao.updateBalance(username, price);
//int i = 10/0;
//new FileInputStream("D://hahahahha.aa");
}
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void updatePrice(String isbn,int price){
bookDao.updatePrice(isbn, price);
}
/**
* 根据业务的特性;进行调整
* isolation=Isolation.READ_UNCOMMITTED:读出脏数据
*
*
* READ_COMMITTED;实际上业务逻辑中用的最多的也是这个;
* REPEATABLEP_READ;
* @param isbn
* @return
*/
@Transactional(readOnly=true)
public int getPrice(String isbn){
return bookDao.getPrice(isbn);
}
@Transactional
public void mulTx(){
//ioc.getBean("BookSerice");
checkout("Tom", "ISBN-001");
updatePrice("ISBN-002", 998);
int i=10/0;
}
}
tx.xml(xml方式)
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<context:component-scan base-package="com.atguigu"></context:component-scan>
<!-- 0、引入外部配置文件 -->
<context:property-placeholder location="classpath:dbconfig.properties" />
<!-- 1、配置数据源 -->
<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
</bean>
<!-- 2、配置JdbcTemplate操作数据库 value="#{pooledDataSource}" ref="pooledDataSource"-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" value="#{pooledDataSource}"></property>
</bean>
<!-- 3、配置声明式事务
1)、Spring中提供事务管理器(事务切面),配置这个事务管理器
2)、开启基于注解的事务式事务;依赖tx名称空间
3)、给事务方法加注解
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="pooledDataSource"></property>
</bean>
<!--
基于xml配置的事务;依赖tx名称空间和aop名称空间
1)、Spring中提供事务管理器(事务切面),配置这个事务管理器
2)、配置出事务方法;
3)、告诉Spring哪些方法是事务方法;
(事务切面按照我们的切入点表达式去切入事务方法)
-->
<aop:config>
<aop:pointcut expression="execution(* com.atguigu.ser*.*.*(..))" id="txPoint"/>
<!-- 事务建议;事务增强 advice-ref:指向事务管理器的配置 -->
<aop:advisor advice-ref="myAdvice" pointcut-ref="txPoint"/>
</aop:config>
<!-- 配置事务管理器; 事务建议;事务增强;事务属性;
transaction-manager="transactionManager":指定是配置哪个事务管理器;
-->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<!--事务属性 -->
<tx:attributes>
<!-- 指明哪些方法是事务方法;
切入点表达式只是说,事务管理器要切入这些方法,
哪些方法加事务使用tx:method指定的 -->
<tx:method name="*"/>
<tx:method name="checkout" propagation="REQUIRED" timeout="-1"/>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 都用;重要的用配置,不重要的用注解 -->
</beans>