当前位置: 首页 > 编程笔记 >

详解Spring Boot 事务的使用

有德业
2023-03-14
本文向大家介绍详解Spring Boot 事务的使用,包括了详解Spring Boot 事务的使用的使用技巧和注意事项,需要的朋友参考一下

spring Boot 使用事务非常简单,首先使用注解 @EnableTransactionManagement 开启事务支持后,然后在访问数据库的Service方法上添加注解 @Transactional 便可。

关于事务管理器,不管是JPA还是JDBC等都实现自接口 PlatformTransactionManager 如果你添加的是 spring-boot-starter-jdbc 依赖,框架会默认注入 DataSourceTransactionManager 实例。如果你添加的是 spring-boot-starter-data-jpa 依赖,框架会默认注入 JpaTransactionManager 实例。

你可以在启动类中添加如下方法,Debug测试,就能知道自动注入的是 PlatformTransactionManager 接口的哪个实现类。

@EnableTransactionManagement // 启注解事务管理,等同于xml配置方式的 <tx:annotation-driven />
@SpringBootApplication
public class ProfiledemoApplication {
 @Bean
 public Object testBean(PlatformTransactionManager platformTransactionManager){
 System.out.println(">>>>>>>>>>" + platformTransactionManager.getClass().getName());
 return new Object();
 }
 public static void main(String[] args) {
 SpringApplication.run(ProfiledemoApplication.class, args);
 }
}

这些SpringBoot为我们自动做了,这些对我们并不透明,如果你项目做的比较大,添加的持久化依赖比较多,我们还是会选择人为的指定使用哪个事务管理器。

代码如下:

@EnableTransactionManagement
@SpringBootApplication
public class ProfiledemoApplication {
 // 其中 dataSource 框架会自动为我们注入
 @Bean
 public PlatformTransactionManager txManager(DataSource dataSource) {
 return new DataSourceTransactionManager(dataSource);
 }
 @Bean
 public Object testBean(PlatformTransactionManager platformTransactionManager) {
 System.out.println(">>>>>>>>>>" + platformTransactionManager.getClass().getName());
 return new Object();
 }
 public static void main(String[] args) {
 SpringApplication.run(ProfiledemoApplication.class, args);
 }
}

在Spring容器中,我们手工注解@Bean 将被优先加载,框架不会重新实例化其他的 PlatformTransactionManager 实现类。

然后在Service中,被 @Transactional 注解的方法,将支持事务。如果注解在类上,则整个类的所有方法都默认支持事务。

对于同一个工程中存在多个事务管理器要怎么处理,请看下面的实例,具体说明请看代码中的注释。

@EnableTransactionManagement // 开启注解事务管理,等同于xml配置文件中的 <tx:annotation-driven />
@SpringBootApplication
public class ProfiledemoApplication implements TransactionManagementConfigurer {
 @Resource(name="txManager2")
 private PlatformTransactionManager txManager2;
 // 创建事务管理器1
 @Bean(name = "txManager1")
 public PlatformTransactionManager txManager(DataSource dataSource) {
 return new DataSourceTransactionManager(dataSource);
 }
 // 创建事务管理器2
 @Bean(name = "txManager2")
 public PlatformTransactionManager txManager2(EntityManagerFactory factory) {
 return new JpaTransactionManager(factory);
 }
 // 实现接口 TransactionManagementConfigurer 方法,其返回值代表在拥有多个事务管理器的情况下默认使用的事务管理器
 @Override
 public PlatformTransactionManager annotationDrivenTransactionManager() {
 return txManager2;
 }
 public static void main(String[] args) {
 SpringApplication.run(ProfiledemoApplication.class, args);
 }
}
@Component
public class DevSendMessage implements SendMessage {
 // 使用value具体指定使用哪个事务管理器
 @Transactional(value="txManager1")
 @Override
 public void send() {
 System.out.println(">>>>>>>>Dev Send()<<<<<<<<");
 send2();
 }
 // 在存在多个事务管理器的情况下,如果使用value具体指定
 // 则默认使用方法 annotationDrivenTransactionManager() 返回的事务管理器
 @Transactional
 public void send2() {
 System.out.println(">>>>>>>>Dev Send2()<<<<<<<<");
 }
}

注:

如果Spring容器中存在多个 PlatformTransactionManager 实例,并且没有实现接口 TransactionManagementConfigurer 指定默认值,在我们在方法上使用注解 @Transactional 的时候,就必须要用value指定,如果不指定,则会抛出异常。

对于系统需要提供默认事务管理的情况下,实现接口 TransactionManagementConfigurer 指定。

对有的系统,为了避免不必要的问题,在业务中必须要明确指定 @Transactional 的 value 值的情况下。不建议实现接口 TransactionManagementConfigurer,这样控制台会明确抛出异常,开发人员就不会忘记主动指定。

参考阅读:

详解SpringBoot的事务管理

Spring Boot 快速入门指南

Spring Boot 快速入门教程

以上所述是小编给大家介绍的Spring Boot 事务的使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对小牛知识库网站的支持!

 类似资料:
  • 本文向大家介绍详解SpringBoot的事务管理,包括了详解SpringBoot的事务管理的使用技巧和注意事项,需要的朋友参考一下 Springboot内部提供的事务管理器是根据autoconfigure来进行决定的。 比如当使用jpa的时候,也就是pom中加入了spring-boot-starter-data-jpa这个starter之后。 Springboot会构造一个JpaTransacti

  • 本文向大家介绍SpringBoot+Dubbo+Seata分布式事务实战详解,包括了SpringBoot+Dubbo+Seata分布式事务实战详解的使用技巧和注意事项,需要的朋友参考一下 前言 Seata 是 阿里巴巴开源的分布式事务中间件,以高效并且对业务0侵入的方式,解决微服务场景下面临的分布式事务问题。 事实上,官方在GitHub已经给出了多种环境下的Seata应用示例项目,地址:https

  • 本文向大家介绍SpringBoot 注解事务声明式事务的方式,包括了SpringBoot 注解事务声明式事务的方式的使用技巧和注意事项,需要的朋友参考一下  springboot 对新人来说可能上手比springmvc要快,但是对于各位从springmvc转战到springboot的话,有些地方还需要适应下,尤其是xml配置。我个人是比较喜欢注解➕xml是因为看着方便,查找方便,清晰明了。但是xm

  • 本文向大家介绍SpringBoot异步任务使用方法详解,包括了SpringBoot异步任务使用方法详解的使用技巧和注意事项,需要的朋友参考一下 步骤,如图所示: 1.添加异步任务业务类 2.添加测试控制器 3.添加启动类 4.右键项目Run As启动,访问url 结果: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍Springboot中@Value的使用详解,包括了Springboot中@Value的使用详解的使用技巧和注意事项,需要的朋友参考一下 Springboot通过@Value注解将配置文件中的属性注入到容器内组件中(可用在@Controller、@Service、@Configuration、@Component等Spring托管的类中)  1.普通字符串注入 例:yml中存在key

  • 本文向大家介绍MySql的事务使用与示例详解,包括了MySql的事务使用与示例详解的使用技巧和注意事项,需要的朋友参考一下 在MySQL中,事务就是一个逻辑工作单元的一系列步骤。事务是用来保证数据操作的安全性。 事务的特征: 1.Atomicity(原子性) 2.Consistency(稳定性,一致性) 3.Isolation(隔离性) 4.Durability(可靠性) 注:事务只针对对数据数据