当前位置:网站首页>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.02.13 - NC004. Print number of loops
- What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
- [brush questions] top101 must be brushed in the interview of niuke.com
- 使用 BR 恢复 S3 兼容存储上的备份数据
- It's hard to find a job when the industry is in recession
- 1202 character lookup
- "Designer universe" APEC design +: the list of winners of the Paris Design Award in France was recently announced. The winners of "Changsha world center Damei mansion" were awarded by the national eco
- 从表中名称映射关系修改视频名称
- 灰度升级 TiDB Operator
- Make learning pointer easier (3)
猜你喜欢
Asia Pacific Financial Media | female pattern ladyvision: forced the hotel to upgrade security. The drunk woman died in the guest room, and the hotel was sentenced not to pay compensation | APEC secur
Sort according to a number in a string in a column of CSV file
NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle
National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund
[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
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
IP lab, the first weekly recheck
IoT -- 解读物联网四层架构
2022.02.13 - NC001. Reverse linked list
Leetcode question brushing (5.28) hash table
随机推荐
使用 BR 备份 TiDB 集群数据到兼容 S3 的存储
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
Zhong Xuegao, who cannot be melted, cannot escape the life cycle of online celebrity products
Introduction to backup and recovery Cr
[research materials] 2022 China yuancosmos white paper - Download attached
【刷题】牛客网面试必刷TOP101
Analysis of pointer and array written test questions
NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle
Asia Pacific Financial Media | female pattern ladyvision: forced the hotel to upgrade security. The drunk woman died in the guest room, and the hotel was sentenced not to pay compensation | APEC secur
Vocabulary notes for postgraduate entrance examination (3)
3. File operation 3-with
"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
Upgrade tidb operator
Erc20 token agreement
Nft智能合约发行,盲盒,公开发售技术实战--拼图篇
LDAP Application Section (4) Jenkins Access
图像融合--挑战、机遇与对策
hcip--mpls
Pyqt5 development tips - obtain Manhattan distance between coordinates
"Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media