当前位置:网站首页>用JpaTransactionManager操作数据库事务
用JpaTransactionManager操作数据库事务
2022-07-01 22:12:00 【march of Time】
狭义上的事务特指数据库事务。它主要是当多个应用程序并发访问数据库时,事务可以在这些应用程序之间提供一个隔离方法,以防止彼此的操作互相干扰
事务为数据库操作序列提供了一个从失败中恢复到正常状态的方法, 同时提供了数据库即使在异常状态下仍能保持数据一致性的方法。
事务具有四个特征,分别是原子性(Atomicity )、一致性(Consistency )、隔离性(Isolation) 和持久性(Durability),简称为事务的ACID特性。
用JpaTransactionManager操作数据库事务的代码:
JpaTransactionManager transactionManager = applicationContext.getBean(JpaTransactionManager.class);
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别,开启新事务,这样会比较安全些。
TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态
Project project;
try {
...(自己的业务逻辑)
transactionManager.commit(status);
} catch (Exception e) {
transactionManager.rollback(status);
log.error("addProject error:{}", ExceptionUtils.getStackTrace(e));
throw new BusinessException("创建项目组失败");
}
publisher.publishEvent(new CreateProjectEvent().setProject(project));
}
边栏推荐
- Today's sleep quality record 71 points
- 半监督学习之数据增强
- Operation category read is not supported in state standby
- Stimulate new kinetic energy and promote digital economy in multiple places
- tcpdump命令使用详解
- MySQL stored procedure
- Share some feelings of a programmer who has experienced layoffs twice a year
- Explain the volatile keyword
- Understanding of indexes in MySQL
- 447 Bili Bili noodles warp 1
猜你喜欢
“信任机器”为发展赋能
Multi picture alert ~ comparison of Huawei ECs and Alibaba cloud ECS
Fiori applications are shared through the enhancement of adaptation project
Selection of all-optical technology in the park - Part 2
思科考试--路由的概念和配置考试
元宇宙可能成为互联网发展的新方向
Appium自动化测试基础 — APPium安装(一)
好友新书发布,祝贺(送福利)
正则系列之组和范围(Groups and Ranges)
Chen Tianqi's machine learning compilation course (free)
随机推荐
What class loading mechanisms does the JVM have?
SAP ui5 application development tutorial 104 - multi select support for SAP ui5 table controls and how to use code to select multiple table row items at a time
Explain the use of locksupport in detail
A few minutes before work, I found out V-model and The difference between sync
软件测试之「 性能测试」总结,新手上路必会知识点
rxjs Observable of 操作符的单步调试分析
Flynk SQL client uses comparison and is familiar with official documents
ECMAScript 2022 正式发布,有你了解过的吗?
多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
Explain ThreadLocal in detail
General use of qstringlist
[literacy] deep / shallow, local / global features in machine learning image processing
[target tracking] | single target tracking indicator
数字货币:影响深远的创新
转--深入LUA脚本语言,让你彻底明白调试原理
[JUC learning road day 8] condition
[untitled]
Happy number [fast and slow pointer of ring PROBLEMS]
SAP UI5 应用开发教程之一百零四 - SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
Origin2018安装教程「建议收藏」