当前位置:网站首页>MySQL learning records 12jdbc operation transactions
MySQL learning records 12jdbc operation transactions
2022-07-06 08:26:00 【Jatine】
List of articles
MySQL Learning record 12JDBC Operational transaction
ACID principle
- Atomicity : Or it's all done , Or it's not done
- Uniformity : The total remains unchanged
- persistence : Once submitted, it's irreversible , Persistent to the database
- Isolation, : Multiple processes do not interfere with each other
The problem of isolation
- Dirty reading : One transaction reads another uncommitted transaction
- It can't be read repeatedly : In the same business , Repeatedly read the data in the table , Table data has changed
- Virtual reading ( Fantasy reading ): In a business , Read the data inserted by others , The results read before and after are inconsistent
Code implementation
- First step : Open transaction (
conn.setAutoCommit(false);) - The second step : After the execution of a group of business , Commit transaction (
conn.commit();) - The third step : Can be in catch Statement to define the rollback statement , But the default failure will also be rolled back (
conn.rollback();)
sql Create table statement :
CREATE TABLE account
(
id Int PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(40),
money FLOAT
);
/* Insert test data */
insert into account(name, money)
values ('A', 1000);
insert into account(name, money)
values ('B', 1000);
insert into account(name, money)
values ('C', 1000);
import com.jatine.lesson02.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestTransaction1 {
public static void main(String[] args) throws SQLException {
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
// Turn off automatic submission of the database , Automatically opens the transaction
conn.setAutoCommit(false); // Open transaction
String sql1 = "update account set money = money - 100 where name = 'A'";
st = conn.prepareStatement(sql1);
st.executeUpdate();
String sql2 = "update account set money = money + 100 where name = 'B'";
st = conn.prepareStatement(sql2);
st.executeUpdate();
// Business finished , Commit transaction
conn.commit();
System.out.println(" success !");
} catch (SQLException e) {
// If it fails, roll back the transaction
try {
conn.rollback(); // Even if there is no such sentence , If it fails, it is also the default rollback
} catch (SQLException throwables) {
throwables.printStackTrace();
}
e.printStackTrace();
} finally {
JdbcUtils.release(conn, st, rs);
}
}
}


Simulate failure scenarios :
String sql1 = "update account set money = money - 100 where name = 'A'";
st = conn.prepareStatement(sql1);
st.executeUpdate();
int x = 1 / 0; // Report errors
String sql2 = "update account set money = money + 100 where name = 'B'";
st = conn.prepareStatement(sql2);
st.executeUpdate();
Report errors :
The data has not changed :
边栏推荐
- sys.argv
- VMware 虚拟化集群
- [research materials] 2021 China online high growth white paper - Download attached
- [MySQL] log
- Use br to back up tidb cluster data to S3 compatible storage
- Chinese Remainder Theorem (Sun Tzu theorem) principle and template code
- Learn Arduino with examples
- [research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached
- Migrate data from a tidb cluster to another tidb cluster
- PLT in Matplotlib tight_ layout()
猜你喜欢

Zhong Xuegao, who cannot be melted, cannot escape the life cycle of online celebrity products
![[untitled]](/img/38/bc025310b9742b5bf0bd28c586ec0d.jpg)
[untitled]

The resources of underground pipe holes are tight, and the air blowing micro cable is not fragrant?

Fibonacci sequence

2022.02.13 - NC003. Design LRU cache structure

C语言自定义类型:结构体

Analysis of pointer and array written test questions

2. File operation - write

【MySQL】数据库的存储过程与存储函数通关教程(完整版)

It's hard to find a job when the industry is in recession
随机推荐
2022.02.13 - NC002. sort
Remote storage access authorization
"Designer universe" Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers | national economic and Informa
使用 TiUP 升级 TiDB
[research materials] 2022 China yuancosmos white paper - Download attached
"Friendship and righteousness" of the center for national economy and information technology: China's friendship wine - the "unparalleled loyalty and righteousness" of the solidarity group released th
备份与恢复 CR 介绍
PLT in Matplotlib tight_ layout()
Hungry for 4 years + Ali for 2 years: some conclusions and Thoughts on the road of research and development
Nft智能合约发行,盲盒,公开发售技术实战--拼图篇
Convolution, pooling, activation function, initialization, normalization, regularization, learning rate - Summary of deep learning foundation
Leetcode question brushing record | 203_ Remove linked list elements
Configuring OSPF load sharing for Huawei devices
C语言自定义类型:结构体
Pointer advanced --- pointer array, array pointer
Asia Pacific Financial Media | "APEC industry +" Western Silicon Valley invests 2trillion yuan in Chengdu Chongqing economic circle to catch up with Shanghai | stable strategy industry fund observatio
Nacos Development Manual
String to leading 0
Migrate data from CSV files to tidb
Yyds dry goods inventory three JS source code interpretation eventdispatcher