当前位置:网站首页>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 :
边栏推荐
- 从 CSV 文件迁移数据到 TiDB
- All the ArrayList knowledge you want to know is here
- Image fusion -- challenges, opportunities and Countermeasures
- LDAP应用篇(4)Jenkins接入
- JS select all and tab bar switching, simple comments
- Beijing invitation media
- 1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
- Golang DNS 随便写写
- [2022 Guangdong saim] Lagrange interpolation (multivariate function extreme value divide and conquer NTT)
- Golang DNS write casually
猜你喜欢

On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced

好用的TCP-UDP_debug工具下载和使用

Learn Arduino with examples

Asia Pacific Financial Media | art cube of "designer universe": Guangzhou community designers achieve "great improvement" in urban quality | observation of stable strategy industry fund

Make learning pointer easier (3)

Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation

IoT -- 解读物联网四层架构

Wincc7.5 download and installation tutorial (win10 system)

C language - bit segment

C语言 - 位段
随机推荐
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
指针和数组笔试题解析
C语言 - 位段
Pyqt5 development tips - obtain Manhattan distance between coordinates
[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached
matplotlib. Widgets are easy to use
Nft智能合约发行,盲盒,公开发售技术实战--合约篇
使用 Dumpling 备份 TiDB 集群数据到兼容 S3 的存储
Day29-t77 & t1726-2022-02-13-don't answer by yourself
IP lab, the first weekly recheck
VMware virtualization cluster
Nft智能合约发行,盲盒,公开发售技术实战--拼图篇
升级 TiDB Operator
C language - bit segment
【MySQL】鎖
Ruffian Heng embedded bimonthly, issue 49
【MySQL】锁
ESP series pin description diagram summary
Upgrade tidb with tiup
1202 character lookup