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

边栏推荐
- Simple solution to phpjm encryption problem free phpjm decryption tool
- Security design verification of API interface: ticket, signature, timestamp
- Mexican SQL manual injection vulnerability test (mongodb database) problem solution
- Not registered via @EnableConfigurationProperties, marked(@ConfigurationProperties的使用)
- CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
- What is the current situation of the game industry in the Internet world?
- 四川云教和双师模式
- Use xtrabackup for MySQL database physical backup
- Software test engineer development planning route
- How to build an interface automation testing framework?
猜你喜欢

MySQL實戰優化高手04 借著更新語句在InnoDB存儲引擎中的執行流程,聊聊binlog是什麼?

实现微信公众号H5消息推送的超级详细步骤

UEditor国际化配置,支持中英文切换

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

The underlying logical architecture of MySQL

Download and installation of QT Creator

Routes and resources of AI

cmooc互联网+教育

Download address of canoe, download and activation of can demo 16, and appendix of all canoe software versions

MySQL实战优化高手02 为了执行SQL语句,你知道MySQL用了什么样的架构设计吗?
随机推荐
Control the operation of the test module through the panel in canoe (Advanced)
C杂讲 双向循环链表
Sichuan cloud education and double teacher model
Southwest University: Hu hang - Analysis on learning behavior and learning effect
C杂讲 浅拷贝 与 深拷贝
美新泽西州州长签署七项提高枪支安全的法案
A new understanding of RMAN retention policy recovery window
UEditor国际化配置,支持中英文切换
15 医疗挂号系统_【预约挂号】
Security design verification of API interface: ticket, signature, timestamp
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.
Zsh configuration file
软件测试工程师必备之软技能:结构化思维
如何让shell脚本变成可执行文件
MySQL实战优化高手10 生产经验:如何为数据库的监控系统部署可视化报表系统?
CDC: the outbreak of Listeria monocytogenes in the United States is related to ice cream products
在CANoe中通過Panel面板控制Test Module 運行(初級)
NLP路线和资源
简单解决phpjm加密问题 免费phpjm解密工具
Ueeditor internationalization configuration, supporting Chinese and English switching