当前位置:网站首页>Dynamically connecting data sources and transaction rollback
Dynamically connecting data sources and transaction rollback
2022-04-21 21:22:00 【Fear of being dominated by baldness】
** demand :** Read data connection from database , Insert some data into a table in the connection , If one of the insertion fails , Then all data will be rolled back .
** Ideas :** Because it is a dynamic configuration data source ,@Transactional Only applicable to the current database connection , And only support spring Built in database connection , therefore @Transactional Easy to fail and troublesome .
solve : java.sql.DriverManager Provides getConnection() Method , adopt getConnection() Create connection Connection, take Connection Is set to false, Reuse Connection Of CreateStatement() Method generation Statement, function SQL sentence , call connection Of commit() Method submission , Use try catch Surround ,catch When an exception occurs, call connection Of rollback() Method rollback ,finally Closed in connection and Statement, complete .
code
Connection connection = null;
Statement statement = null;
try {
connection = DriverManager.getConnection(url,username,password);// Create connection
connection .setAutoCommit=false;// Set not to submit automatically , Otherwise, only the wrong piece of data will be rolled back
statement = connection.createStatement();
for () {
// Loop execution SQL
String sql = "";
statement.executeUpdate(sql);// function sql sentence
}
connection.commit();// Commit transaction
} catch (Exception e) {
connection.rollback();// Transaction rollback , The current transactions under this connection will be rolled back
e.printStackTrace();
return false;
} finally {
statement.close();
connection.close();// Close the connection
}
版权声明
本文为[Fear of being dominated by baldness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212106379231.html
边栏推荐
- Smart face recognition 2 - keras uses retinaface + face to build a face recognition platform
- 工作杂谈——浅谈数据岗位
- Sword finger offer 15 Number of 1 in binary
- Gan remake 2, which seems to be fun -- keras built srgan platform to improve the super-resolution of pictures
- 解决国内手机无法注册Google gmail账户问题
- 7-3 银行业务队列简单模拟|PTA
- 迅为RK3568开发板交叉编译C程序
- Others - Analysis of redis and MySQL double write consistency scheme
- 其它——MyCat实现分库分表
- 太原理工大学程序设计竞赛团队2022年特大丰收
猜你喜欢

产品增长框架:从 Web2 到 Web3

Application case of Lora wireless data transmission module of Internet of things: lorawan gateway communication technology

聪明的人脸识别3——Pytorch 搭建自己的Facenet人脸识别平台

Others - MYCAT realizes sub database and sub table

奇安信冬奥“零事故”终端安全分享会:成功解决6641次攻击事件

通达OA系统对接 单点登录平台使用和开发手册

Neural network learning small record 56 -- the principle and function of batch normalization layer

Han Pi's semantic segmentation and remaking 6 -- pytoch builds its own UNET semantic segmentation platform

32位电脑和64位电脑

其它——Redis与Mysql双写一致性方案解析
随机推荐
UART learning
Back to basics, multi-party security computing should return to the original consideration of "security"
Red sun shooting range -- intranet penetration practice
Others - understand CGI, fastcgi, WSGI, uwsgi and uwsgi
flutter插件第三方库,给Android程序员的一些面试建议
Manuel d'utilisation et de développement de la plate - forme de connexion unique pour l'amarrage du système d'AP de Tongda
Yuanxin technology seeks to be listed again: the amount of loss soars, and the R & D expense rate is less than 2%. How to break through?
神经网络学习小记录56——Batch Normalization层的原理与作用
841. 字符串哈希 (字符串哈希模板)
工作流 流程设置 定制开发
Bailian3752 maze [BFS]
Others - Analysis of redis and MySQL double write consistency scheme
【sql】SQL22 统计各个部门的工资记录数
Others - Introduction to Devops
25. < tag array and simulation > - LT - 31 Next permutation + LT - 556 Next larger element III
聪明的人脸识别2——Keras 利用Retinaface+Facenet搭建人脸识别平台
841. String hash (string hash template)
Behind flight safety, the indispensable wireless communication sensor equipment of Internet of things
Others - zerorpc and simplexmlrpserver
Kubernetes详解(五)——Kubernetes核心对象