文章目录
声明式事务
XML配置方式
配置事务管理器
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
配置AOP通知(advice)
简单示例:
<!-- 示例演示给所有get、find、query开头的方法添加只读事务,其他方法是默认事务(可读可写) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
< tx:advice / >默认配置如下:
- 传播行为(propagation)是REQUIRED
- 隔离级别(isolation level)是DEFAULT
- 事务默认可读写(read-write)
- 事务超时时间是数据库默认事务超时时间
- unchecked异常RuntimeException异常触发事务回滚,checked异常Exception不会触发回滚
< tx:method / > 设置项:
属性 | 是否必须 | 默认值 | 描述 |
---|---|---|---|
name | Yes | 与事务属性关联的方法名称。支持通配符(如, get, handle*, on*Event等 | |
propagation | No | REQUIRED | 事务传播行为 |
isolation | No | DEFAULT | 事务隔离级别 仅当传播行为设置为 REQUIRED 或者 REQUIRES_NEW时有效. |
timeout | No | -1 | 事务超时时间(单位:秒). 仅当传播行为设置为 REQUIRED 或者 REQUIRES_NEW时有效. |
read-only | No | false | 设置读写事务或者只读事务. 仅当传播行为设置为 REQUIRED 或者 REQUIRES_NEW时有效. |
rollback-for | No | 设置多个可以触发事务回滚的异常(多个用英文逗号隔开). 如,com.foo.MyBusinessException,ServletException. | |
no-rollback-for | No | 设置多个禁止触发事务回滚的异常(多个用英文逗号隔开).如,com.foo.MyBusinessException,ServletException. |
示例
tx_transaction.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.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">
<!-- 通过xml方式配置spring声明式事务 配置开始 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="studentDao" class="com.lanou3g.spring.transaction.xml.dao.StudentDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<!-- 第一步:配置数据源 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="connectionProperties" value="${jdbc.characterEncoding}" />
</bean>
<!-- 第二步:初始化事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 第三步:配置事务AOP通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="insert*" rollback-for="Exception"/>
<tx:method name="query*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 第四步:定义AOP配置(将上面的通知和表达式组装到一起) -->
<aop:config>
<aop:pointcut id="all_dao_method" expression="execution(* com.lanou3g.spring.transaction.xml.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="all_dao_method" />
</aop:config>
<!-- 通过xml方式配置spring声明式事务 配置结束 -->
</beans>
jdbc.properties数据源
jdbc.url=jdbc:mysql://localhost:3306/zzj0301
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
jdbc.characterEncoding=UTF-8
数据库对应的java类
@Getter
@Setter
public class Student {
private int id;
private String sname;
private int age;
private String gender;
private String nickname;
}
对数据库进行操作的java类
@Setter
@Getter
public class StudentDaoImpl {
private JdbcTemplate jdbcTemplate;
/**
* 插入一条student数据
* @param map
* @return 返回影响行数
*/
public int insertStudent(Map map){
Object[] args = new Object[]{
map.get("sname"),
map.get("age"),
map.get("gender"),
map.get("nickname")
};
int row = jdbcTemplate.update("insert into student (sname,age,gender,nickname) values (?,?,?,?)",args);
//模拟事务异常,使程序回滚
//int a = 2 / 0;
//返回影响行数
return row;
}
}
程序运行入口
public class AppXML {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("tx_transaction.xml");
Map<String,Object> map = new HashMap<>();
map.put("sname","王五");
map.put("age",42);
map.put("gender","男");
map.put("nickname","小五");
int row = ctx.getBean(StudentDaoImpl.class).insertStudent(map);
System.out.println("影响了" + row + "行");
}
}
注解配置方式
开启事务注解支持
XML中开启:
<!-- 开启事务注解扫描 -->
<!-- 如果定义的事务管理器名称就叫transactionManager,则此属性可以省略 -->
<tx:annotation-driven transaction-manager="txManager" />
注解方式开启:
@Configuration
@EnableTransactionManagement
public class MyTransactionConf {
}
配置事务管理器
/**
* 通过注解的方式配置Spring声明式事务
* 也可以将事务管理器定义到xml中,注解事务会按类型自动找到xml中配置的事务管理器
*/
@Configuration
public class MyTransactionConf {
/**
* 定义TransactionManager bean
* @param dataSource
* @return
*/
@Bean
public PlatformTransactionManager txManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
在需要事物的方法上添加事务注解
// 给批量插入方法添加事务控制
@Transactional
@Override
public int[] batchInsertStudent(List<Student> studentList) {
// 具体操作....
}
// 给查询方法添加只读事务
@Transactional(readOnly = true)
public List<Student> queryStudents() {
// 具体操作
}
@Transactional属性说明
- value: 指定特定的事务管理器,默认是transactionManager
- 其他属性和xml中的的属性类似
示例
jdbc.properties数据源
jdbc.url=jdbc:mysql://localhost:3306/zzj0301
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
jdbc.characterEncoding=UTF-8
配置数据源
@PropertySource("classpath:jdbc.properties")
@Component
public class MyDataSource extends DriverManagerDataSource {
public MyDataSource(@Value("${jdbc.driver}") String driver,@Value("${jdbc.url}") String url,@Value("${jdbc.user}") String username,@Value("${jdbc.password}") String password,@Value("${jdbc.characterEncoding}") String characterEncoding) {
super.setDriverClassName(driver);
super.setUrl(url);
super.setUsername(username);
super.setPassword(password);
Properties properties = new Properties();
properties.setProperty("characterEncoding",characterEncoding);
super.setConnectionProperties(properties);
}
}
数据库对应的java类
@Getter
@Setter
public class Student {
private int id;
private String sname;
private int age;
private String gender;
private String nickname;
}
对数据库进行操作的java类
@Setter
@Getter
@Component
public class StudentDaoImpl {
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 插入一条student数据
* @param map
* @return 影响的行数
*/
@Transactional(rollbackFor = {Exception.class})
public int insertStudent(Map map){
Object[] args = new Object[]{
map.get("sname"),
map.get("age"),
map.get("gender"),
map.get("nickname")
};
int row = jdbcTemplate.update("insert into student (sname,age,gender,nickname) values (?,?,?,?)",args);
//模拟事务异常,使程序回滚
//int a = 2 / 0;
return row;
}
}
代码运行入口
@Configuration
@ComponentScan(basePackages = "com.lanou3g.spring.transaction.annotation")
@EnableTransactionManagement //开启事务相关注解支持
public class App {
/**
* 定义JdbcTemplate对象,Spring给我们封装了所有的JDBC操作
* @param dataSource
* @return
*
* 相当于xml配置中的
* <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
* <property name="dataSource" ref="dataSource" />
* </bean>
*/
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
/**
*定义事务管理器
* @param dataSource
* @return
*
* 相当于xml配置中的
* <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
* <property name="dataSource" ref="dataSource" />
* </bean>
*/
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource){ //DataSourceTransactionManager是PlatformTransactionManager的一个实现类
return new DataSourceTransactionManager(dataSource);
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(App.class);
Map<String,Object> map = new HashMap<>();
map.put("sname","欧阳锋");
map.put("age",42);
map.put("gender","男");
map.put("nickname","西毒");
int row = ctx.getBean(StudentDaoImpl.class).insertStudent(map);
System.out.println("影响了" + row + "行");
}
}
回滚事务
回滚特定异常(rollback-for)
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" rollback-for="NoProductInStockException"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
不回滚特定异常(no-rollback-for)
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="updateStock" no-rollback-for="InstrumentNotFoundException"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
spring事务的隔离级别和传播行为
七种传播行为
1. PROPAGATION_REQUIRED – 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
2. PROPAGATION_SUPPORTS – 支持当前事务,如果当前没有事务,就以非事务方式执行。
3. PROPAGATION_MANDATORY – 支持当前事务,如果当前没有事务,就抛出异常。
4. PROPAGATION_REQUIRES_NEW – 新建事务,如果当前存在事务,把当前事务挂起。
5. PROPAGATION_NOT_SUPPORTED – 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
6. PROPAGATION_NEVER – 以非事务方式执行,如果当前存在事务,则抛出异常。
7. PROPAGATION_NESTED – 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。
备注:常用的两个事务传播属性是1和4,即PROPAGATIONREQUIRED PROPAGATIONREQUIRES_NEW
五种隔离级别
隔离级别 | 说明 |
---|---|
ISOLATION_DEFAULT | 这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别. |
ISOLATIONREADUNCOMMITTED | 这是事务最低的隔离级别,它允许另外一个事务可以看到这个事务未提交的数据。 这种隔离级别会产生脏读,不可重复读和幻像读。 |
ISOLATIONREADCOMMITTED | 保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。 这种事务隔离级别可以避免脏读出现,但是可能会出现不可重复读和幻像读。 |
ISOLATIONREPEATABLEREAD | 这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。 它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。 |
ISOLATION_SERIALIZABLE | 这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。 除了防止脏读,不可重复读外,还避免了幻像读。 |
关键词:
幻读(虚读)
事务1读取记录时事务2增加了记录并提交,事务1再次读取时可以看到事务2新增的记录;
通俗的说,幻读就是指在一个事务内读取了别的事务插入的数据,导致前后读取不一致(insert)
不可重复读取
事务1读取记录时,事务2更新了记录并提交,事务1再次读取时可以看到事务2修改后的记录;
在一个事务内读取表中的某一行数据,多次读取结果不同.一个事务读取到了另一个事务提交后的数据.
脏读
事务1更新了记录,但没有提交,事务2读取了更新后的行,然后事务T1回滚,现在T2读取无效。
通俗的说,脏读就是指一个事务读取了一个未提交事务的数据
其他业务场景
统一异常处理、日志记录