时间:2026-02-10 11:39
人气:
作者:admin
在一个业务流程中,需要多条DML(insert、delete、update)语句联合才能完成。这些语句必须同时成功或者同时失败。这样才能保证数据安全。
多条DML同时成功或者同时失败,叫做事务。
spring实现事务的2种方式:
spring对事务的管理是基于aop实现的。所以spring专门针对事务开发了一套api,其核心接口如下:PlatformTransactionManager 接口。
需要配置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.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" >
<!-- 组件扫描-->
<context:component-scan base-package="com.ali" />
<!-- 配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/bank"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
</bean>
<!-- 配置jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务注解驱动器,开启事务注解,需要加上tx的命名空间-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
可以在类和方法上加@Transactional 开启事务
什么是事务的传播行为?
在service种有a()和b()2个方法,a()上有事务,b()上也有事务,当a()在执行过程中调用了b(),事务是如何传递的?是合并到一个事务?还是开启一个新事务?这就是事务的传播行为。
一共有7种传播行为:
在代码中设置事务的传播行为:
@Transactional(propagation = Propagation.MANDATORY)
数据库中读取数据存在三大问题:
事务的隔离级别有4个:
MySQL默认可重复读,Oracle默认读提交
仅在读的事务中设置隔离级别就行,写的事务没必要设置
代码中设置事务的隔离级别:
@Transactional(isolation = Isolation.DEFAULT)
@Transactional(timeout = 10)
以上代码设置事务超时时间为10s
表示超过10s,如果事务中所有的DML语句还没有执行完毕的话,最终结果会回滚。
默认值-1,表示没有时间限制。
事务的超时时间值得是哪段时间?
在当前事务中,最后一条DML语句执行之前的时间,如果最后一条DML语句后面有很多业务逻辑,这些业务代码执行的时间不被计入超时时间。
@Transactional(readOnly = true)
将当前事务设为只读事务,在该事务中只允许执行select 语句。
该特性的作用是:启动spring的优化策略。提高select语句的执行效率。
@Transactional(rollbackFor = RuntimeException.class)
表示发生RuntimeException异常或该异常的子类异常才回滚
@Transactional(noRollbackFor = NullPointerException.class)
表示发生NullPointerException异常或该异常的子类不回滚,其他异常才回滚
编写配置类
@Configuration // 代替xml配置文件
@ComponentScan("com.ali") // 扫描com.ali包下的所有类
@EnableTransactionManagement // 开启事务管理
public class Spring6Config {
// @Bean注解用于将方法的返回值注册为Spring容器中的一个Bean
@Bean(name = "druidDataSource")
public DruidDataSource druidDataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl("jdbc:mysql://localhost:3306/spring6?useSSL=false&serverTimezone=UTC");
druidDataSource.setUsername("root");
druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
druidDataSource.setPassword("123456");
return druidDataSource;
}
@Bean(name = "transactionManager")
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
@Bean(name = "jdbcTemplate")
// 该方法的参数DataSource dataSource会自动从Spring容器中找到类型为DataSource的Bean并注入
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
}
使用时和其他方式一样。
本文来自博客园,作者:NE_STOP,转载请注明原文链接:https://www.cnblogs.com/alineverstop/p/19598826