spring boot開啟事務管理,使用事務的回滾機制,使兩條插入語句一致
阿新 • • 發佈:2018-05-21
value nbsp tcl true 管理 配置 AI let dao
spring boot 事務管理,使用事務的回滾機制
1:配置事務管理
在springboot 啟動類中添加
@EnableTransactionManagement //開啟事務管理 @EnableAsync(proxyTargetClass=true) //配置代理為cglib代理,默認使用 的是jdk動態代理
2:配置管理器
package com.li; import javax.sql.DataSource; @EnableAutoConfiguration @SpringBootApplication @MapperScan("com.li.dao") @EnableTransactionManagement @EnableAsync(proxyTargetClass=true) public class SpringBootLabApplication { public static void main(String[] args){ SpringApplication.run(SpringBootLabApplication.class, args); } // 創建事務管理器1 @Bean(name = "txManager1") //給事務管理器命名 public PlatformTransactionManager txManager(DataSource dataSource) {return new DataSourceTransactionManager(dataSource); } }
3:在@Service類的方法上添加 @Transactional(value="事務名")
@Transactional(value="txManager1") @Override public boolean insert(InsertParameter parameter) throws Exception { //如果想讓兩條插入語句共同執行,異常一定要拋出 int i1=manageMapper.insertNewsList(parameter);;int i2=manageMapper.insertNews(parameter); if (i1!=i2 || i1!=1) { return false; } return true; }
4:ok
spring boot開啟事務管理,使用事務的回滾機制,使兩條插入語句一致