当前位置:网站首页>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
边栏推荐
- A deep learning model for urban traffic flow prediction with traffic events mined from twitter
- Memcached comprehensive analysis – 3 Deletion mechanism and development direction of memcached
- Suspend component and asynchronous component
- VSCode无网环境快速迁移开发环境(VIP典藏版)
- leetcode-201_2021_10_17
- 【Camera基础(一)】Camera摄像头工作原理及整机架构
- 煮茶论英雄!福建省发改委、市营商办领导一行莅临育润大健康事业部交流指导
- socket(2)
- Structured interview of state-owned enterprises and central enterprises employment of state-owned enterprises Modou Interactive Employment Service steward
- 【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
猜你喜欢
二叉搜索树模板
心楼:华为运动健康的七年筑造之旅
2022国际女性工程师日:戴森设计大奖彰显女性设计实力
如何做到全彩户外LED显示屏节能环保
Antdb database online training has started! More flexible, professional and rich
[camera Foundation (I)] working principle and overall structure of camera
Understanding openstack network
升哲科技 AI 智能防溺水服务上线
使用region折叠代码
《各行业零代码企业应用案例集锦》正式发布
随机推荐
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
leetcode1720_2021-10-14
面试官:你说你精通Redis,你看过持久化的配置吗?
机器学习:梯度下降法
MySQL optimizes query speed
[camera Foundation (I)] working principle and overall structure of camera
建木持续集成平台v2.5.0发布
机器学习:线性回归
AntDB数据库在线培训开课啦!更灵活、更专业、更丰富
Intelligent fish tank control system based on STM32 under Internet of things
socket done
滤波数据分析
为什么生命科学企业都在陆续上云?
平衡二叉搜索树
Memcached comprehensive analysis – 2 Understand memcached memory storage
手动事务的几个类
Tournament sort
【吴恩达笔记】卷积神经网络
Why are life science enterprises on the cloud in succession?
BBR bandwidth per second conversion logic