当前位置:网站首页>Transaction rollback and record exception information at the same time
Transaction rollback and record exception information at the same time
2022-07-26 20:49:00 【root_ zhb】
Transaction rollback , Record abnormal information at the same time
demand
When an exception occurs , Rollback the operation on the database , At the same time, insert an exception message into the database .
Realization principle
Start a new business , Perform database operation of abnormal information
Realization
1、 rely on HTTP Resend request
Before throwing an exception , rely on http, Send an asynchronous request , An interface stores exception information , Then the main program throws an exception .
2、 Start a new business , Insert exception information
@Autowired
private DataSourceTransactionManager dstManager;
@Transactional
@Override
public void create(Payment payment) throws Exception {
try {
paymentDao.create(payment);
System.out.println(1 / 0);
} catch (Exception e) {
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
// Here you can add exception thrown information , Does not affect the existing logic
} finally {
// Isolation level of things , Start a new business , It'll be safer .
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
// Get transaction state
TransactionStatus transaction = dstManager.getTransaction(def);
try {
payment.setSerial(" Report an error, Lala Lala ");
paymentDao.create(payment);
dstManager.commit(transaction);
} catch (Exception e) {
dstManager.rollback(transaction);
logger.error(" Temporary table insertion failed :" + e.getMessage(), e);
}
}
}
A way of implementation , The same idea as the above code
@Transactional
@Override
public void create(Payment payment) {
paymentDao.create(payment);
paymentService.newTransCreate(payment);
System.out.println(1 / 0);
}
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void newTransCreate(Payment payment) {
payment.setSerial(" This is a new error message ");
paymentDao.create(payment);
}
边栏推荐
- Experiment 5 OSPF comprehensive experiment
- Correct the classpath of your application so that it contains compatible versions of the classes com
- 美国应用材料公司宣布将以22亿美元现金收购国际电气
- Robin Lee was "poured cold water" enthusiasm! Baidu allied with Huawei Kirin to release "Honghu" chip
- Build etcd distributed storage system +web management interface from scratch
- Correct the classpath of your application so that it contains compatible versions of the classes com
- Solve the horizontal segmentation of iBGP and BGP routing principles
- It is said that HP / Dell / Microsoft / Amazon are considering transferring some hardware production lines outside the mainland
- APP自动化测试框架搭建(八)--ATX Server2多设备集群环境搭建
- 解决IBGP的水平分割和BGP选路原则
猜你喜欢

Buu brush inscription - WANGDING cup column 2

BGP routing black hole and anti ring

BTC和ETH不确定性增强 因加息逼近?美国经济将面临更多痛苦

Leetcode-300 最长递增子序列

Software testing - development test content specification (project test template)

Quick start to connection pooling

The sandbox cooperates with artist Alec monopoly

Buu brush inscription 2

员工辞职还得赔偿公司损失?34岁机长辞职被公司索赔1066万

Houdini 求中点,点连成线
随机推荐
Chat software project development 2
leetcode 哈希表类
分组卷积(Group Converlution)
The lawyer team of the US Department of justice asked the judge to refuse to accept Huawei's lawsuit
Easy gene | introduction to macrovirus sequencing technology
81. (cesium home) cesium modifies the gray background (default blue)
软件测试-开发提测内容规范(项目提测模板)
08_ue4进阶_开始结束暂停菜单等ui
Execution context and Lexical Environment
Opencv DNN deployment onnx model
884. Uncommon words in two sentences - hash table
BGP--边界网关协议
Auto. JS rotation Icon
培训软件测试能不能就业
confd+etcd实现高可用自动发现
Houdini 求中点,点连成线
What is the role of cache in the storage system of data blocks?
BUU刷题记2
BGP routing black hole and anti ring
Why can ThreadLocal achieve thread isolation?