当前位置:网站首页>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();
}
}
}
}

边栏推荐
- Routes and resources of AI
- 颜值爆表,推荐两款JSON可视化工具,配合Swagger使用真香
- 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
- MySQL实战优化高手07 生产经验:如何对生产环境中的数据库进行360度无死角压测?
- 软件测试工程师必备之软技能:结构化思维
- [after reading the series of must know] one of how to realize app automation without programming (preparation)
- MySQL實戰優化高手08 生產經驗:在數據庫的壓測過程中,如何360度無死角觀察機器性能?
- Set shell script execution error to exit automatically
- 安装OpenCV时遇到的几种错误
- History of object recognition
猜你喜欢

寶塔的安裝和flask項目部署

CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
![17 medical registration system_ [wechat Payment]](/img/b4/f9abfa0fb0447d727078069d888b57.png)
17 medical registration system_ [wechat Payment]

Download and installation of QT Creator

Redis集群方案应该怎么做?都有哪些方案?

Super detailed steps to implement Wechat public number H5 Message push

西南大学:胡航-关于学习行为和学习效果分析

A necessary soft skill for Software Test Engineers: structured thinking

MySQL combat optimization expert 03 uses a data update process to preliminarily understand the architecture design of InnoDB storage engine

Sichuan cloud education and double teacher model
随机推荐
flask运维脚本(长时间运行)
C miscellaneous lecture continued
oracle sys_ Context() function
MySQL combat optimization expert 10 production experience: how to deploy visual reporting system for database monitoring system?
Control the operation of the test module through the panel in canoe (Advanced)
History of object recognition
MySQL real battle optimization expert 11 starts with the addition, deletion and modification of data. Review the status of buffer pool in the database
MySQL实战优化高手12 Buffer Pool这个内存数据结构到底长个什么样子?
MySQL combat optimization expert 03 uses a data update process to preliminarily understand the architecture design of InnoDB storage engine
再有人问你数据库缓存一致性的问题,直接把这篇文章发给他
MySQL实战优化高手04 借着更新语句在InnoDB存储引擎中的执行流程,聊聊binlog是什么?
Mexican SQL manual injection vulnerability test (mongodb database) problem solution
14 medical registration system_ [Alibaba cloud OSS, user authentication and patient]
Use xtrabackup for MySQL database physical backup
Download and installation of QT Creator
西南大学:胡航-关于学习行为和学习效果分析
The replay block of canoe still needs to be combined with CAPL script to make it clear
text 文本数据增强方法 data argumentation
A necessary soft skill for Software Test Engineers: structured thinking
MySQL combat optimization expert 12 what does the memory data structure buffer pool look like?