spring - 我已将@Transactional 放入方法中,但它仍会在回滚之前提交事务
问题描述
Spring中如何实现事务管理
我的方法如下
@Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
@Override
public InwardDTO saveEntity(InwardDTO entity) throws Exception {
try {
costCalculation(entity);
InwardDTO dto = super.saveEntity(entity);
addStock(dto.getDetails(), dto.getId());
return dto;
} catch (Exception ex) {
throw ex;
}
}
private void addStock(Set<InwardDetailsDTO> argDetailsDTOSet, Long argInwardId) throws RuntimeException {
String SUBMODULE = getModuleNameForLog() + " [addStock()] ";
if (1 == 1) {
throw new RuntimeException("Test Case");
}
}
日志就像,
2020-02-29 15:01:14.210 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.TransactionInterceptor : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
Hibernate: insert into tbl_inward_chemical (date, invoice_number, is_deleted, party_id, po_id, pre_inward_id_id, remark, slip, total_amount, total_weight) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into tbl_inward_details_chemical (inward_id, is_deleted, is_pass, party_moisture_id, party_ph_id, party_price, party_purity_id, party_solubility_id, party_weight, product_name_id, received_moisture_id, received_ph_id, received_price, received_purity_id, received_solubility_id, received_weight, total_price) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into tbl_inward_details_chemical (inward_id, is_deleted, is_pass, party_moisture_id, party_ph_id, party_price, party_purity_id, party_solubility_id, party_weight, product_name_id, received_moisture_id, received_ph_id, received_price, received_purity_id, received_solubility_id, received_weight, total_price) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2020-02-29 15:01:14.216 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.TransactionInterceptor : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
2020-02-29 15:01:14.216 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.TransactionInterceptor : Completing transaction for [com.alignedorg.chemical.inward.service.InwardService.saveEntity] after exception: java.lang.RuntimeException: Test Case
2020-02-29 15:01:14.216 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.RuleBasedTransactionAttribute : Applying rules to determine whether transaction should rollback on java.lang.RuntimeException: Test Case
2020-02-29 15:01:14.216 TRACE 14504 --- [nio-8080-exec-2] o.s.t.i.RuleBasedTransactionAttribute : Winning rollback rule is: RollbackRuleAttribute with pattern [java.lang.RuntimeException]
2020-02-29 15:01:14.222 ERROR 14504 --- [nio-8080-exec-2] c.a.core.utillity.log.ApplicationLogger : [ Inward Controller ] [SAVE] Test Case
java.lang.RuntimeException: Test Case
在此事务中始终在 addStock 方法回滚之前提交......在日志中它显示为事务正在回滚但条目保存在数据库中......
解决方案
Spring事务bean必需列表:
1.Jdbc模板
@Bean(name = "jdbcTemplate")
public JdbcTemplate creatJdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
2.数据源
@Bean(name = "dataSource")
public DataSource creatDataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(driverClassName);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
3.事务管理器
@Bean(name = "transactionManager")
public PlatformTransactionManager creatTransactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
4.@EnableTransactionManagement
以上1个@Configuration Class
激活TxManager
5.使用JdbcTemplate的instance( @Autowired
) inDaoImplements
来执行sql with query()
|| update()
方法
推荐阅读
- discord.js - 检测消息中是否有表情符号。不和谐.js
- sql - SQL中按日期跨日期范围的瀑布
- python - OpenCV 和来自 m3u8 url 的实时视频流视频
- sql - [SQL]ORACLE - 按具有相同 ID 的数据和行排序
- node.js - 使用 React.js 或 Next.js 创建和更新静态 HTML 页面?
- amazon-web-services - 如何为 S3 访问日志目标存储桶创建具有 sourceIP 限制的存储桶策略?
- python - 提高 Python 对象比较的性能
- amazon-web-services - AWS lambda 运行 sagemaker 笔记本,但通过终端导入时出错
- python - 为什么我的递归数独求解器不起作用?
- python - 从系列数据中过滤整数