Spring5事务管理

2022-11-10,

事务管理是什么?

相当于过滤器,如果这一进程中上一个操作正常执行完后提交数据已经发生改变,但是下一个操作中出现了异常,这样就会影响数据的查看。

典型例子:银行转账,甲方已经转钱给乙方(甲方已扣钱),乙方收款时出现异常(没有收到钱),程序还是执行完了,所以这是错误的。

操作事务

注解@Transactional

propagation:事务传播行为

ioslation:事务隔离级别

(1)事务有特性成为隔离性,多事务操作之间不会产生影响。不考虑隔离性产生很多问题

(2)有三个读问题:脏读、不可重复读、虚(幻)读

    脏读:一个未提交事务读取到另一个未提交事务的数据(如果甲方将50修改到了500,但是出现异常事务回滚,乙方看到的是甲方修改的500)

    不可重复读:一个未提交事务读取到另一提交事务修改数据

    虚读:一个未提交事务读取到另一提交事务添加数据

(3)虚读:一个未提交事务读取到另一提交事务添加数据

timeout:超时时间

(1)事务需要在一定时间内进行提交,如果不提交进行回滚

(2)默认值是 -1 ,设置时间以秒单位进行计算

readOnly:是否只读

(1)读:查询操作,写:添加修改删除操作

(2)readOnly 默认值 false,表示可以查询,可以添加修改删除操作

(3)设置 readOnly 值是 true,设置成 true 之后,只能查询

rollbackFor:回滚

(1)设置出现哪些异常进行事务回滚

noRollbackFor:不回滚

(1)设置出现哪些异常不进行事务回滚

//readOnly:只读,timeout:超过时间,propagation:事务传播行为,isolateion:事务隔离级别
@Transactional(readOnly = false,timeout = -1,propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ)//配置事务
@Service
public class UserService {
//注入dao
@Autowired
private UserDao userDao;
public void accountMoney(){
// try {
//第一步 开启事务
//第二步 进行业务操作
//少100
userDao.reduce();
//模拟异常
// int i = 10/0;
//多100
userDao.add();
//第三步 没有发生异常,提交事务
// }catch(Exception e) {
//第四步 出现异常,事务回滚
// }
}
}

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: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/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"> <!-- 组件扫描 -->
<context:component-scan base-package="com.spring5"></context:component-scan> <!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="jdbc:mysql:///jspzy"/>
<property name="username" value="root" />
<property name="password" value="a1814995041" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
</bean> <!-- JdbcTemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入dataSource-->
<property name="dataSource" ref="dataSource"></property>
</bean> <!--创建事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean> <!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

完全注解声明式事务管理

@Configuration//设置配置类
@ComponentScan(basePackages = "com.spring5")//开始组件扫描
@EnableTransactionManagement//开始事务
public class TxConfig {
//创建数据库连接池
@Bean
public DruidDataSource getDruidDataSource(){
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///jspzy");
dataSource.setUsername("root");
dataSource.setPassword("a1814995041");
return dataSource;
}
@Bean
public JdbcTemplate getJdbcTemplate(DataSource dataSource){
JdbcTemplate jdbcTemplate = new JdbcTemplate();//到 ioc 容器中根据类型找到 dataSource
jdbcTemplate.setDataSource(dataSource);//注入dataSource
return jdbcTemplate;
}
@Bean
public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource){
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
}

Spring5事务管理的相关教程结束。

《Spring5事务管理.doc》

下载本文的Word格式文档,以方便收藏与打印。