@EnableTransactionManagement的使用
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,这样控制台会明确抛出异常,开发人员就不会忘记主动指定。
@EnableTransactionManagement的使用的更多相关文章
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-001-使用Hibernate(@Inject、@EnableTransactionManagement、@Repository、PersistenceExceptionTranslationPostProcessor)
一.结构 二.Repository层 1. package spittr.db; import java.util.List; import spittr.domain.Spitter; /** * ...
- @EnableTransactionManagement注解理解
@EnableTransactionManagement表示开启事务支持,在springboot项目中一般配置在启动类上,效果等同于xml配置的<tx:annotation-driven /&g ...
- Spring Boot的事务管理注解@EnableTransactionManagement的使用
@EnableTransactionManagement:负责开启springboot 的事物支持,等同于xml配置文件中的 <tx:annotation-driven /> 然后在访问数 ...
- @EnableConfigurationProperties、@EnableAsync、 @EnableTransactionManagement
★@ConfigurationProperties和@EnableConfigurationProperties配合使用 @ConfigurationProperties注解主要用来把properti ...
- Annotation Type EnableTransactionManagement
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Ena ...
- 【spring】spring boot中使用@EnableTransactionManagement 以后,spring mvc接收前台ajax的post方法传过来的参数,使用@RequestBody接收不到参数
在启动类上添加了注解: @EnableTransactionManagement, postMan测试接口,以这种方式传递参数: 测试结果: 接收不到参数 问题解决: 原因:是因为 这个项目中的Con ...
- spring 事务 @EnableTransactionManagement原理
@EnableXXX原理:注解上有个XXXRegistrar,或通过XXXSelector引入XXXRegistrar,XXXRegistrar实现了 ImportBeanDefinitionRegi ...
- 【spring cloud】@EnableTransactionManagement注解的意义
@EnableTransactionManagement注解的意义
- 阶段一-01.万丈高楼,地基首要-第2章 单体架构设计与准备工作-2-27 为何不使用@EnableTransactionManagement就能使用事务?
使用了注解使用事务.但是没有开启注解的启用 启动类里面使用注解 @EnableTransactionManager开启事物的管理. 为什么我们没有开启这个注解,还需要在响应的Service里面使用事务 ...
随机推荐
- SQL logic error no such module: fts5 解决方案
因项目原因,需要使用SQLite的全文索引,用到了最新的fts5模块 但在咱们.net framwork中却会提示“SQL logic error no such module: fts5”:找不到f ...
- Array + two points leetcode.15-3Sum
题面 Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Fi ...
- flex布局实战
1.实现盒子的水平垂直居中 .parent{ width:200px; height:200px; display:flex; align-items: center; justify-content ...
- SuperMemo method
原文:https://www.supermemo.com/en/archives1990-2015/english/ol/sm2 别人的研究:http://wdxtub.lofter.com/post ...
- MYSQL的操作命令
一.御前 1 win+R DOS 输入 net start mtsql 和 net stop mysql 启动和停止Mysql 服务,也可通过计算机——管理——服务和应用程序——服务——MYSQL— ...
- 关于PPP拨号 和 AT指令实现GPRS模块联网的疑问
以下内容摘抄自互联网: ppp拨号 与 at命令的疑问 GPRS模块在Linux平台上ppp拨号上网总结与心得 以PPP拨号实现GPRS与因特网的数据通信的具体实现流程 问: 我刚接触GPRS,了解A ...
- 从win到多系统
相信有不少电脑爱好者喜欢折腾系统,尤其还是一个小白(感觉多系统强的不要不要的,各种崇拜),然后就走上了深渊. 首先,我一开始也是个win系统的忠实用户,没用过其他系统的我几乎不知道其他系统的存在,反正 ...
- 0007SpringBoot配置不同环境内容及指定启动哪个环境
1.多profiles的形式 分别新增application-dev.properties和application-prod.properties配置文件, 其中application-dev.pro ...
- sqlserver常用运维sql
1. sqlserver 检测sql SELECT top 10 (total_elapsed_time / execution_count)/1000 N'平均时间ms' ,total_elapse ...
- 2019HDU多校Minimal Power of Prime——分段讨论&&思维
题目 将 $n$($1 < n \leq 10^{18}$)质因数分解,求质因数幂的最小值. 分析 直接质因数分解,不太行. 可以这样想,对小区间质因数分解,n变小了,再枚举答案. 打印1-10 ...