当前位置:网站首页>Use JUnit unit test & transaction usage
Use JUnit unit test & transaction usage
2022-07-06 10:19:00 【Artificial intelligence cockroach】
Use Junit unit testing & Use of transactions
Junit unit testing
@Test Annotations are only valid in methods , As long as you add this annotation , You can run it directly .(import org.junit.Test)
Business
Only when everything is right , Before committing the transaction , The database changes accordingly , Ensure the safety of data operation .
1. 1. 1. Open transaction
// Notify the database to open the transaction ,false Turn on
connection.setAutoCommit(false);
2. 2. 2. Transaction submission
connection.commit();
3. 3. 3. Transaction rollback
// If there is an anomaly , Tell the database to roll back the transaction
connection.rollback();
4. 4. 4. Closing transaction
connection.close();
example ( bank transfer )
1. 1. 1. Use 1/0 Expressions to make mistakes ;
2. 2. 2. Do not open transaction , Transfer failed and cannot be rolled back , That is, both parties lost the money transferred ;
3. 3. 3. Open transaction , Through try-catch Roll back , Transfer between both parties failed , Maintain the original state .
package com.tl.study01;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/** * @author tl * Business Roll back Junit unit testing */
public class TestJdbc04 {
@Test
public void test() {
// Configuration information
//useUnicode=true&characterEncoding=utf-8 Solve the Chinese garbled code
String url="jdbc:mysql://localhost:3306/jdbc_study?useUnicode=true&characterEncoding=utf-8";
String username = "root";
String password = "123";
Connection connection = null;
//1. The load driver
try {
Class.forName("com.mysql.jdbc.Driver");
//2. Connect to database , On behalf of the database
connection = DriverManager.getConnection(url, username, password);
//3. Notify the database to open the transaction ,false Turn on
connection.setAutoCommit(false);
String sql = "update account set money = money-100 where name = 'A'";
connection.prepareStatement(sql).executeUpdate();
// Making mistakes
int i = 1/0;
String sql2 = "update account set money = money+100 where name = 'B'";
connection.prepareStatement(sql2).executeUpdate();
connection.commit();// Two of the above SQL It's all done , Just commit the business !
System.out.println("success");
} catch (Exception e) {
try {
// If there is an anomaly , Tell the database to roll back the transaction
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

边栏推荐
- Pointer learning
- The 32 year old programmer left and was admitted by pinduoduo and foreign enterprises. After drying out his annual salary, he sighed: it's hard to choose
- Control the operation of the test module through the panel in canoe (Advanced)
- 寶塔的安裝和flask項目部署
- 颜值爆表,推荐两款JSON可视化工具,配合Swagger使用真香
- The governor of New Jersey signed seven bills to improve gun safety
- Good blog good material record link
- C杂讲 文件 续讲
- C miscellaneous shallow copy and deep copy
- Redis集群方案应该怎么做?都有哪些方案?
猜你喜欢

112 pages of mathematical knowledge sorting! Machine learning - a review of fundamentals of mathematics pptx

Installation de la pagode et déploiement du projet flask

The replay block of canoe still needs to be combined with CAPL script to make it clear

Security design verification of API interface: ticket, signature, timestamp

Super detailed steps to implement Wechat public number H5 Message push

华南技术栈CNN+Bilstm+Attention

MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计

解决在window中远程连接Linux下的MySQL

Export virtual machines from esxi 6.7 using OVF tool

Introduction tutorial of typescript (dark horse programmer of station B)
随机推荐
MySQL实战优化高手12 Buffer Pool这个内存数据结构到底长个什么样子?
Tianmu MVC audit I
百度百科数据爬取及内容分类识别
Installation of pagoda and deployment of flask project
cmooc互联网+教育
Control the operation of the test module through the panel in canoe (Advanced)
Bugku web guide
Sichuan cloud education and double teacher model
ZABBIX introduction and installation
MySQL實戰優化高手04 借著更新語句在InnoDB存儲引擎中的執行流程,聊聊binlog是什麼?
Implement context manager through with
Retention policy of RMAN backup
vscode 常用的指令
14 medical registration system_ [Alibaba cloud OSS, user authentication and patient]
NLP路线和资源
MySQL combat optimization expert 06 production experience: how does the production environment database of Internet companies conduct performance testing?
Contrôle de l'exécution du module d'essai par panneau dans Canoe (primaire)
安装OpenCV时遇到的几种错误
Inject common SQL statement collation
Ueeditor internationalization configuration, supporting Chinese and English switching