当前位置:网站首页>Several classes of manual transactions
Several classes of manual transactions
2022-06-24 21:52:00 【Jingling cat】
The main categories
TransactionManager
PlatformTransactionManager

TransactionStatus

TransactionDefinition,TransactionTemplate

Example
@Service
public class MyService{
@Autowired
DataSourceTransactionManager transactionManager;
public ResultMap lockStockWhenNewOrder(List<StockChangeByOrderDto> goodsModels) {
//2. Get transaction definition
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
//3. Set the transaction isolation level , Start a new business
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
//4. Get transaction state , Equivalent to opening things
TransactionStatus transactionStatus = transactionManager.getTransaction(def);
try{
//insert or update ...
transactionManager.commit(transactionStatus);
} catch (InterruptedException e) {
transactionManager.rollback(transactionStatus);
}
}
}
Create a tool class that controls transactions , And to spring management
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
@Component
public class TransationUtils {
@Autowired
private PlatformTransactionManager platformTransactionManager;
private TransactionStatus transactionStatus;
/** * Open transaction * * @return */
public TransactionStatus beginTransaction() {
transactionStatus = platformTransactionManager.getTransaction(new DefaultTransactionDefinition());
return transactionStatus;
}
/** * Commit transaction */
public void commitTransaction() {
if (!transactionStatus.isCompleted()) {
platformTransactionManager.commit(transactionStatus);
}
}
/** * Roll back the transaction */
public void rollbackTransaction() {
if (!transactionStatus.isCompleted()) {
platformTransactionManager.rollback(transactionStatus);
}
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
// Programming transactions ( Manual required begin Manual rollback Hand in )
@Component
public class TransactionUtils {
// Get transaction source
@Autowired
private DataSourceTransactionManager dataSourceTransactionManager;
// Open transaction
public TransactionStatus begin() {
TransactionStatus transaction = dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute());
return transaction;
}
// Commit transaction
public void commit(TransactionStatus transaction) {
dataSourceTransactionManager.commit(transaction);
}
// Roll back the transaction
public void rollback(TransactionStatus transaction) {
dataSourceTransactionManager.rollback(transaction);
}
}
Inject the tool class into the class that needs to be used for transactions , And use
@Autowired
TransationUtils transationUtils;
public void test(){
// Open transaction
transationUtils.beginTransaction();
try {
·············
// The operation successfully committed the transaction
transationUtils.commitTransaction();
} catch (Exception e) {
// Catch exception rollback transaction
transationUtils.rollbackTransaction();
throw new RuntimeException(" Failure !");
}
}
The method of automatically starting a transaction , The principle is to judge whether the transaction ends normally under various circumstances , If an exception is caught, roll back , If normal, submit
Annotate the business @Transactional
springboot You can use annotations directly on the method @Transactional(rollbackFor = Exception.class)
Turn on transactions automatically , When an exception occurs, it will be rolled back automatically , however If you use try catch Transactions fail when , need Manually roll back the transaction , The specific operation is as follows
try {
······················
}catch (Exception e) {
// Manual rollback
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}
Reference resources
Spring Business
Manual transactions and multithreaded transactions
边栏推荐
- leetcode1863_2021-10-14
- TypeScript快速入门
- 123. the best time to buy and sell shares III
- Blender's simple skills - array, rotation, array and curve
- Interpretation of ebpf sockops code
- 字节的软件测试盆友们你们可以跳槽了,这还是你们心心念念的字节吗?
- Datakit 代理实现局域网数据统一汇聚
- how to install clustershell
- 如何做到全彩户外LED显示屏节能环保
- The collection of zero code enterprise application cases in various industries was officially released
猜你喜欢

Vscode netless environment rapid migration development environment (VIP collection version)

关于Unity中的transform.InverseTransformPoint, transform.InverseTransofrmDirection

虚拟机CentOS7中无图形界面安装Oracle(保姆级安装)
Visit Amazon memorydb and build your own redis memory database

2022国际女性工程师日:戴森设计大奖彰显女性设计实力

memcached全面剖析–2. 理解memcached的內存存儲

【吴恩达笔记】多变量线性回归
![[featured] how do you design unified login with multiple accounts?](/img/df/9b4fc11a6971ebe8162ae84250a782.png)
[featured] how do you design unified login with multiple accounts?

福建省发改委福州市营商办莅临育润大健康事业部指导视察工作

Sslhandshakeexception: no subject alternative names present - sslhandshakeexception: no subject alternative names present
随机推荐
Direct attack on "three summers" production: good harvest news spreads frequently and summer broadcasting is in full swing
Blender's landscape
(待补充)GAMES101作业7提高-实现微表面模型你需要了解的知识
suspense组件和异步组件
TKKC round#3
Pattern recognition - 9 Decision tree
EasyBypass
A deep learning model for urban traffic flow prediction with traffic events mined from twitter
PKI notes
66 pitfalls in go programming language: pitfalls and common errors of golang developers
【Camera基础(一)】Camera摄像头工作原理及整机架构
SAP接口debug设置外部断点
VSCode无网环境快速迁移开发环境(VIP典藏版)
Kubernetes 集群中流量暴露的几种方案
Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
Prompt that the device has no permission when using ADB to connect to the device
Transport layer UDP & TCP
memcached全面剖析–2. 理解memcached的內存存儲
直击“三夏”生产:丰收喜报频传 夏播紧锣密鼓
我国SaaS产业的发展趋势与路径