当前位置:网站首页>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 :
边栏推荐
- 2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
- Configuring OSPF load sharing for Huawei devices
- CAD ARX 获取当前的视口设置
- ESP系列引脚說明圖匯總
- 【云原生】手把手教你搭建ferry开源工单系统
- [2022 广东省赛M] 拉格朗日插值 (多元函数极值 分治NTT)
- 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
- PLT in Matplotlib tight_ layout()
- C language - bit segment
- Understanding of law of large numbers and central limit theorem
猜你喜欢

Understanding of law of large numbers and central limit theorem
![[cloud native] teach you how to build ferry open source work order system](/img/fb/507f763791235bd00bc8201e5d7741.png)
[cloud native] teach you how to build ferry open source work order system

Pointer advanced --- pointer array, array pointer

"Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media

Golang DNS 随便写写

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

Learn Arduino with examples

面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
![[MySQL] lock](/img/ce/9f8089da60d9b3a3f92a5e4eebfc13.png)
[MySQL] lock

Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
随机推荐
化不掉的钟薛高,逃不出网红产品的生命周期
1202 character lookup
Leetcode question brushing record | 203_ Remove linked list elements
[2022 广东省赛M] 拉格朗日插值 (多元函数极值 分治NTT)
Leetcode question brushing (5.28) hash table
NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle
Pointer advanced --- pointer array, array pointer
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
Wincc7.5 download and installation tutorial (win10 system)
Nacos Development Manual
[MySQL] log
What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
LDAP application (4) Jenkins access
Easy to use tcp-udp_ Debug tool download and use
C语言 - 位段
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
Let the bullets fly for a while
Fibonacci sequence
Migrate data from a tidb cluster to another tidb cluster
Introduction to backup and recovery Cr