当前位置:网站首页>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();
}
}
}
}
边栏推荐
- Docker MySQL solves time zone problems
- Sichuan cloud education and double teacher model
- 实现以form-data参数发送post请求
- Several errors encountered when installing opencv
- C miscellaneous two-way circular linked list
- 如何搭建接口自动化测试框架?
- Security design verification of API interface: ticket, signature, timestamp
- MySQL storage engine
- MySQL实战优化高手05 生产经验:真实生产环境下的数据库机器配置如何规划?
- Time complexity (see which sentence is executed the most times)
猜你喜欢
MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计
基于Pytorch的LSTM实战160万条评论情感分类
History of object recognition
四川云教和双师模式
What is the current situation of the game industry in the Internet world?
Solve the problem of remote connection to MySQL under Linux in Windows
Typescript入门教程(B站黑马程序员)
Export virtual machines from esxi 6.7 using OVF tool
再有人问你数据库缓存一致性的问题,直接把这篇文章发给他
AI的路线和资源
随机推荐
Vh6501 Learning Series
MySQL实战优化高手04 借着更新语句在InnoDB存储引擎中的执行流程,聊聊binlog是什么?
Complete web login process through filter
高并发系统的限流方案研究,其实限流实现也不复杂
MySQL实战优化高手07 生产经验:如何对生产环境中的数据库进行360度无死角压测?
Control the operation of the test module through the panel in canoe (Advanced)
Retention policy of RMAN backup
13 medical registration system_ [wechat login]
好博客好资料记录链接
Random notes
14 medical registration system_ [Alibaba cloud OSS, user authentication and patient]
Target detection -- yolov2 paper intensive reading
The appearance is popular. Two JSON visualization tools are recommended for use with swagger. It's really fragrant
Windchill配置远程Oracle数据库连接
[after reading the series of must know] one of how to realize app automation without programming (preparation)
15 医疗挂号系统_【预约挂号】
Sichuan cloud education and double teacher model
华南技术栈CNN+Bilstm+Attention
C杂讲 浅拷贝 与 深拷贝
Mexican SQL manual injection vulnerability test (mongodb database) problem solution