当前位置:网站首页>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 :
边栏推荐
- MFC sends left click, double click, and right click messages to list controls
- [2022 广东省赛M] 拉格朗日插值 (多元函数极值 分治NTT)
- Chinese Remainder Theorem (Sun Tzu theorem) principle and template code
- 2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
- 2022.02.13 - NC003. Design LRU cache structure
- Learn Arduino with examples
- Leetcode question brushing record | 203_ Remove linked list elements
- "Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
- Colorlog结合logging打印有颜色的日志
- 远程存储访问授权
猜你喜欢
![[MySQL] database stored procedure and storage function clearance tutorial (full version)](/img/27/e775e03b77c7195216bc50c5cbefb4.png)
[MySQL] database stored procedure and storage function clearance tutorial (full version)
![[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached](/img/35/898a8086bc35462b0fcb9e6b58b86b.jpg)
[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached
![[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

IP lab, the first weekly recheck

NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle

tree树的精准查询

Easy to use tcp-udp_ Debug tool download and use

What is the use of entering the critical point? How to realize STM32 single chip microcomputer?

Golang DNS write casually

It's hard to find a job when the industry is in recession
随机推荐
化不掉的钟薛高,逃不出网红产品的生命周期
Leetcode question brushing record | 203_ Remove linked list elements
logback1.3. X configuration details and Practice
Leetcode question brushing (5.28) hash table
Day29-t77 & t1726-2022-02-13-don't answer by yourself
Huawei cloud OBS file upload and download tool class
Nacos Development Manual
华为云OBS文件上传下载工具类
The State Economic Information Center "APEC industry +" Western Silicon Valley will invest 2trillion yuan in Chengdu Chongqing economic circle, which will surpass the observation of Shanghai | stable
Pointer advanced --- pointer array, array pointer
Grayscale upgrade tidb operator
Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform
PLT in Matplotlib tight_ layout()
Yyds dry goods inventory three JS source code interpretation eventdispatcher
From monomer structure to microservice architecture, introduction to microservices
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
灰度升级 TiDB Operator
IP lab, the first weekly recheck
Asia Pacific Financial Media | designer universe | Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers
Vocabulary notes for postgraduate entrance examination (3)