一、Spring5+事务
1、事务
A.基本概念
事务是数据库操作的基本单元,逻辑上的一组操作,要么都成功,要么都失败,必须从一个状态到另一个一致性状态。
B.四大特性
ACID,原子性、一致性、隔离性、持久性。
事务特性的详解
2、声明式事务管理
底层就是AOP,进行面向切面编程。
1)基于注解
A)配置事务管理器
B)开启事务注解
C)在Service类上面加事务注解
<?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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 事务管理-->
<bean id="transaction" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transaction"></tx:annotation-driven>
<!-- 配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/db"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
</beans>
package com.xhu.aop;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class ServiceImpl {
}
2)基于XML
step:配置事务管理器 - > 配置通知(这里指事务管理器的事务管理) -> 配置切面(这里面配置切入点并把切入点和通知关联)。
<!-- 事务管理-->
<bean id="transaction" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置通知(这里即事务)及通知连接点,主要配置给那些方法能够切入,即入选可加事务的行列-->
<tx:advice id="advice" transaction-manager="transaction">
<tx:attributes>
<tx:method name="insert*" isolation="REPEATABLE_READ"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务切面,主要配置切入点并将切入点和通知关联起来-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="point" expression="execution(* com.xhu.aop.ServiceImpl.*(..))"/>
<!-- 将切入点和通知关联起来,即配置事务切面-->
<aop:advisor advice-ref="advice" pointcut-ref="point"></aop:advisor>
</aop:config>
<!-- 配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/db"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
3)完全注解开发
package com.xhu.aop;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
/**
* @Configuration 告诉spring这是一个配置类,向Spring声明自己的身份。
*/
@Configuration
//spring知道其为配置类,开始读取下面的配置
@ComponentScan(basePackages = {"com.xhu.aop"})//通过这个注解知道要去扫描注解来创建各组件的bean
@EnableAspectJAutoProxy//通过这个注解来识别AspectJ类,并可以自动代理切入点的方法。
@EnableTransactionManagement//AOP的应用,通过这个注解来识别需要事务管理的类,即事务增强的类或方法。
public class SpringConfig {
//在配置事务增强的类之前,需要先配置增强类所需要的dataSource
@Bean
public DataSource getDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/db");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
//配置事务增强的类,即通知类,即事务管理器.上面创建了DataSource 的bean,所以下面通过根据类型去自动注入就可以
@Bean
public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) {
DataSourceTransactionManager dtm = new DataSourceTransactionManager(dataSource);
return dtm;
}
}
package com.xhu.aop;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class ServiceImpl {
}
3、声明式事务的参数配置
1)propagation,事务传播行为。
两个方法之间的调用,而两个方法可能加了事务或未加事务,如何处理?
2)isolation,事务隔离级别。
多事务的并发带来三个读问题,脏读、不可重复读、幻读。一般设为Repeatable-read
3)timeout,超时时间。
事务需要在一定时间内提交,超时就回滚。默认值-1,即不超时。
4)readyOnly,是否只读。
默认未false,可读也可增删改。
5)rollbackFor,事务回滚。
设置出现哪些异常,事务需要进行回滚。
6)noRollbackFor,事务不回滚。
设置出现哪些异常,事务不用回滚。
参考文献
[1]Spring5 尚硅谷