SpringBoot事务注解详解
SpringBoot事务注解详解
@Transactional
spring 事务注解
1.简单开启事务管理
@EnableTransactionManagement // 启注解事务管理,等同于xml配置⽅式的 <tx:annotation-driven />
2.事务注解详解
默认遇到throw new RuntimeException(“…”);会回滚
需要捕获的throw new Exception(“…”);不会回滚
指定回滚
@Transactional(rollbackFor=Exception.class)
public void methodName() {
// 不会回滚
throw new Exception("...");
}
指定不回滚
@Transactional(noRollbackFor=Exception.class)
public ItimDaoImpl getItemDaoImpl() {
// 会回滚
throw new RuntimeException("注释");
}
如果有事务,那么加⼊事务,没有的话新建⼀个(不写的情况下)
@Transactional(propagation=Propagation.REQUIRED)
容器不为这个⽅法开启事务
@Transactional(propagation=Propagation.NOT_SUPPORTED)
readOnly=true只读,不能更新,删除
@Transactional (propagation = Propagation.REQUIRED,readOnly=true)
设置超时时间
@Transactional (propagation = Propagation.REQUIRED,timeout=30)
设置数据库隔离级别
@Transactional (propagation = Propagation.REQUIRED,isolation=Isolation.DEFAULT)
3.指定事务管理器
spring Boot 使⽤事务⾮常简单,⾸先使⽤注解 @EnableTransactionManagement 开启事务⽀持后,然后在访问数据库的Service⽅法上添加注解 @Transactional 便可。
关于事务管理器,不管是JPA还是JDBC等都实现⾃接⼝ PlatformTransactionManager 如果你添加的是 spring-boot-starter-jdbc 依赖,框架会默认注⼊ DataSourceTransactionManager 实例。如果你添加的是 spring-boot-starter-data-jpa 依赖,框架会默认注⼊JpaTransactionManager 实例。
你可以在启动类中添加如下⽅法,Debug测试,就能知道⾃动注⼊的是 PlatformTransactionManager 接⼝的哪个实现类。
3.1 打印项⽬事务管理器
@EnableTransactionManagement // 启注解事务管理,等同于xml配置⽅式的 <tx:annotation-driven />
@SpringBootApplication
public class ProfiledemoApplication {
@Bean
public Object testBean(PlatformTransactionManager platformTransactionManager){
System.out.println(">>>>>>>>>>" + Class().getName());
return new Object();
}
public static void main(String[] args) {
SpringApplication.run(ProfiledemoApplication.class, args);
}
}
这些SpringBoot为我们⾃动做了,这些对我们并不透明,如果你项⽬做的⽐较⼤,添加的持久化依赖⽐较多,我们还是会选择⼈为的指定使⽤哪个事务管理器。
代码如下:spring roll怎么读
3.2 指定事务管理器
@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(">>>>>>>>>>" + Class().getName());
return new Object();
}
public static void main(String[] args) {
SpringApplication.run(ProfiledemoApplication.class, args);
}
}
内容来⾃:
在Spring容器中,我们⼿⼯注解@Bean 将被优先加载,框架不会重新实例化其他的 PlatformTransactionManager 实现类。
然后在Service中,被 @Transactional 注解的⽅法,将⽀持事务。如果注解在类上,则整个类的所有⽅法都默认⽀持事务。
对于同⼀个⼯程中存在多个事务管理器要怎么处理,请看下⾯的实例,具体说明请看代码中的注释。
3.1 使⽤指定的事务管理器
@EnableTransactionManagement // 开启注解事务管理,等同于xml配置⽂件中的 <tx:annotation-drive
n />
@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()<<<<<<<<");
}
}
内容来⾃:wwwblogs/kesimin/p/9546225.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。