当前位置:网站首页>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();
}
}
}
}
边栏推荐
- Use xtrabackup for MySQL database physical backup
- MySQL Real Time Optimization Master 04 discute de ce qu'est binlog en mettant à jour le processus d'exécution des déclarations dans le moteur de stockage InnoDB.
- 15 医疗挂号系统_【预约挂号】
- MySQL combat optimization expert 12 what does the memory data structure buffer pool look like?
- History of object recognition
- 西南大学:胡航-关于学习行为和学习效果分析
- 高并发系统的限流方案研究,其实限流实现也不复杂
- MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计
- UEditor国际化配置,支持中英文切换
- Redis集群方案应该怎么做?都有哪些方案?
猜你喜欢
Automation sequences of canoe simulation functions
Write your own CPU Chapter 10 - learning notes
Contest3145 - the 37th game of 2021 freshman individual training match_ C: Tour guide
MySQL real battle optimization expert 11 starts with the addition, deletion and modification of data. Review the status of buffer pool in the database
The programming ranking list came out in February. Is the result as you expected?
Installation of pagoda and deployment of flask project
Implement sending post request with form data parameter
AI的路线和资源
Pytorch LSTM实现流程(可视化版本)
Complete web login process through filter
随机推荐
The programming ranking list came out in February. Is the result as you expected?
实现以form-data参数发送post请求
Several silly built-in functions about relative path / absolute path operation in CAPL script
Docker MySQL solves time zone problems
解决在window中远程连接Linux下的MySQL
Simple solution to phpjm encryption problem free phpjm decryption tool
西南大学:胡航-关于学习行为和学习效果分析
使用OVF Tool工具从Esxi 6.7中导出虚拟机
13 medical registration system_ [wechat login]
宝塔的安装和flask项目部署
MySQL Real Time Optimization Master 04 discute de ce qu'est binlog en mettant à jour le processus d'exécution des déclarations dans le moteur de stockage InnoDB.
C杂讲 文件 初讲
ZABBIX introduction and installation
百度百科数据爬取及内容分类识别
Notes of Dr. Carolyn ROS é's social networking speech
Not registered via @EnableConfigurationProperties, marked(@ConfigurationProperties的使用)
Canoe cannot automatically identify serial port number? Then encapsulate a DLL so that it must work
Complete web login process through filter
Pointer learning
Zsh configuration file