当前位置:网站首页>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
边栏推荐
- Sslhandshakeexception: no subject alternative names present - sslhandshakeexception: no subject alternative names present
- 队列实现原理和应用
- TDengine可通过数据同步工具 DataX读写
- [camera Foundation (I)] working principle and overall structure of camera
- AntDB数据库在线培训开课啦!更灵活、更专业、更丰富
- Pattern recognition - 0 introduction
- 平衡二叉搜索树
- The collection of zero code enterprise application cases in various industries was officially released
- Bld3 getting started UI
- 66 pitfalls in go programming language: pitfalls and common errors of golang developers
猜你喜欢

Blender FAQs

C语言-关键字1

2022 international women engineers' Day: Dyson design award shows women's design strength

多路转接select

Bld3 getting started UI

【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index

Remove the screen recording reminder (seven cattle cloud demo)

openGauss内核:简单查询的执行

123. the best time to buy and sell shares III

Fuzhou business office of Fujian development and Reform Commission visited the health department of Yurun university to guide and inspect the work
随机推荐
ST表+二分
【无标题】
VSCode无网环境快速迁移开发环境(VIP典藏版)
Interpretation of ebpf sockops code
Pattern recognition - 0 introduction
C语言-关键字1
Based on asp Net development of fixed assets management system source code enterprise fixed assets management system source code
Antdb database online training has started! More flexible, professional and rich
suspense组件和异步组件
【吴恩达笔记】卷积神经网络
socket done
Kubernetes 集群中流量暴露的几种方案
Li Kou daily question - day 26 -496 Next larger element I
leetcode-201_2021_10_17
About transform InverseTransformPoint, transform. InverseTransofrmDirection
Multi view function in blender
TKKC round#3
LeetCode-513. 找树左下角的值
Implementation of adjacency table storage array of graph
Application practice | massive data, second level analysis! Flink+doris build a real-time data warehouse scheme