当前位置:网站首页>用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));
}
边栏推荐
- Digital currency: far-reaching innovation
- 转--利用C语言中的setjmp和longjmp,来实现异常捕获和协程
- map容器
- Turn -- bring it and use it: share a gadget for checking memory leaks
- 软件测试之「 性能测试」总结,新手上路必会知识点
- Mysql database detailed learning tutorial
- SAP GUI 里的收藏夹事务码管理工具
- Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
- 使用 EMQX Cloud 实现物联网设备一机一密验证
- 447 Bili Bili noodles warp 1
猜你喜欢

Appium自动化测试基础 — 补充:Desired Capabilities参数介绍

激发新动能 多地发力数字经济

SAP GUI 里的收藏夹事务码管理工具

MySQL -- index of InnoDB storage engine

好友新书发布,祝贺(送福利)

Réimpression de l'article csdn

El input text field word limit, beyond which the display turns red and input is prohibited

思科考试--冗余网络

Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle

Cut noodles C language
随机推荐
Turn -- bring it and use it: share a gadget for checking memory leaks
Origin2018 installation tutorial "recommended collection"
[C language] detailed explanation of malloc function [easy to understand]
SAP GUI 里的收藏夹事务码管理工具
Using securecrtportable to remotely connect virtual machines
[JUC learning road day 9] barrier derivatives
软件测试之「 性能测试」总结,新手上路必会知识点
Understanding of transactions in MySQL
[daily training] 66 add one-tenth
Explain JMM in detail
转--原来gdb的底层调试原理这么简单
LC501. Mode in binary search tree
每日刷题记录 (十)
转--利用C语言中的setjmp和longjmp,来实现异常捕获和协程
El input text field word limit, beyond which the display turns red and input is prohibited
cvpr2022 human pose estiamtion
Explain ThreadLocal in detail
多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
Genicam gentl standard ver1.5 (4) Chapter 5 acquisition engine
Use and function of spark analyze command map join broadcast join