当前位置:网站首页>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
边栏推荐
猜你喜欢
leetcode:1504. 统计全 1 子矩形的个数
Analyse complète Memcached – 2. Comprendre le stockage de mémoire pour Memcached
66 pitfalls in go programming language: pitfalls and common errors of golang developers
SAP接口debug设置外部断点
在每个树行中找最大值[分层遍历之一的扩展]
[camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture
【吴恩达笔记】卷积神经网络
Blender's simple skills - array, rotation, array and curve
Network layer & IP
Advanced secret of xtransfer technology newcomers: the treasure you can't miss mentor
随机推荐
【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index
Understanding openstack network
Fuzhou business office of Fujian development and Reform Commission visited the health department of Yurun university to guide and inspect the work
Slider控制Animator动画播放进度
将二维数组方阵顺时针旋转90°
Multiplexer select
AntDB数据库在线培训开课啦!更灵活、更专业、更丰富
Tdengine can read and write through dataX
Vscode netless environment rapid migration development environment (VIP collection version)
dp问题集
Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
Advanced secret of xtransfer technology newcomers: the treasure you can't miss mentor
(to be added) games101 job 7 improvement - knowledge you need to know to realize micro surface model
leetcode1863_2021-10-14
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
ping: www.baidu. Com: unknown name or service
XTransfer技术新人进阶秘诀:不可错过的宝藏Mentor
C语言实现DNS请求器
How to achieve energy conservation and environmental protection of full-color outdoor LED display
心楼:华为运动健康的七年筑造之旅