当前位置:网站首页>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 :
边栏推荐
- Tidb backup and recovery introduction
- 【MySQL】鎖
- [untitled]
- Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
- 根据csv文件某一列字符串中某个数字排序
- Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform
- Sort according to a number in a string in a column of CSV file
- 使用 BR 恢复 S3 兼容存储上的备份数据
- "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
- How to use information mechanism to realize process mutual exclusion, process synchronization and precursor relationship
猜你喜欢
![[MySQL] lock](/img/ce/9f8089da60d9b3a3f92a5e4eebfc13.png)
[MySQL] lock

Zhong Xuegao, who cannot be melted, cannot escape the life cycle of online celebrity products

将 NFT 设置为 ENS 个人资料头像的分步指南

Analysis of pointer and array written test questions

Golang DNS 随便写写

Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center

Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform

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

Easy to use tcp-udp_ Debug tool download and use

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
matplotlib. Widgets are easy to use
Chinese Remainder Theorem (Sun Tzu theorem) principle and template code
Beijing invitation media
从表中名称映射关系修改视频名称
The Vice Minister of the Ministry of industry and information technology of "APEC industry +" of the national economic and information technology center led a team to Sichuan to investigate the operat
从 TiDB 集群迁移数据至另一 TiDB 集群
备份与恢复 CR 介绍
String to leading 0
MFC 给列表控件发送左键单击、双击、以及右键单击消息
All the ArrayList knowledge you want to know is here
IOT -- interpreting the four tier architecture of the Internet of things
ESP series pin description diagram summary
[2022 Guangdong saim] Lagrange interpolation (multivariate function extreme value divide and conquer NTT)
It's hard to find a job when the industry is in recession
MFC sends left click, double click, and right click messages to list controls
从 SQL 文件迁移数据到 TiDB
NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle
使用 BR 恢复 S3 兼容存储上的备份数据
NFT smart contract release, blind box, public offering technology practice -- contract