当前位置:网站首页>事务回滚,同时记录异常信息
事务回滚,同时记录异常信息
2022-07-26 19:11:00 【root_zhb】
事务回滚,同时记录异常信息
需求
在发生异常时,回滚对数据库的操作,同时在数据库中插入一条异常信息。
实现原理
开启新的事务,进行异常信息的数据库操作
实现
1、依赖HTTP重新发送请求
在抛出异常前,依赖http,发送一条异步请求,由一个接口保存异常信息,然后主程序再抛出异常。
2、开启新事务,插入异常信息
@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();
//此处可添加抛出异常信息,不影响现有逻辑
} finally {
// 事物隔离级别,开启新事务,这样会比较安全些。
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
// 获得事务状态
TransactionStatus transaction = dstManager.getTransaction(def);
try {
payment.setSerial("报错啦啦啦啦啦啦");
paymentDao.create(payment);
dstManager.commit(transaction);
} catch (Exception e) {
dstManager.rollback(transaction);
logger.error("临时表插入失败:" + e.getMessage(), e);
}
}
}
一种实现方式,与上面的代码思想是一样的
@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("这是一个新的报错信息");
paymentDao.create(payment);
}
边栏推荐
- Solution to the third game of 2022 Niuke multi school league
- 金仓数据库 KingbaseES SQL 语言参考手册 (14. SQL语句:COMMIT 到 CREATE LANGUAGE)
- 靠元宇宙和NFT,天下秀疯狂“割韭菜”?
- The authentication type 10 is not supported
- 原 iOS面试题收集
- Linux 定时备份数据库并删除 N 天以前的数据
- [PHP] common header definitions
- There are six ways to help you deal with the simpledateformat class, which is not a thread safety problem
- PHP 替换中文字符的方法
- Linux regularly backs up the database and deletes the data n days ago
猜你喜欢

银行业概览

Overview of canvas

eadiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to c

带你熟悉云网络的“电话簿”:DNS

3万脱发人,撑起一个IPO

Training embedded representation of large categories using triple loss and twin neural networks

Docker使用mysql:5.6和 owncloud 镜像,构建一个个人网盘,安装搭建私有仓库 Harbor

Where can I find the files downloaded from iPad

Detailed explanation of Yolo v1

【PHP】将 SESSION 数据保存到 Redis
随机推荐
These 22 drawing (visualization) methods are very important and worth collecting!
【JVM 系列】JVM 调优
企业数字化转型成大趋势,选对在线协作工具很重要
【OBS】Dropped Frames And General Connection Issues
移动端video兼容你需要知道的几点
【实习经验】异常处理与访问url结果响应数据处理
中天钢铁在 GPS、 AIS 调度中使用 TDengine
[PHP] save session data to redis
Deeply analyze the execution process of worker threads in the thread pool through the source code
Ten sorting details
一文看懂中国的金融体系
Kingbases SQL language reference manual of Jincang database (15. SQL statement: create materialized view to create schema)
Detailed explanation of Yolo V2
Use of load balancing
启牛是证券公司的渠道吗开户安全吗?
[binary tree] balance the binary search tree
Uiobject2 of uiautomator2 common classes
6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
plsql包
Scope in JS