当前位置:网站首页>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 :
边栏推荐
- JS select all and tab bar switching, simple comments
- 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
- MFC 给列表控件发送左键单击、双击、以及右键单击消息
- 1204 character deletion operation (2)
- Golang DNS 随便写写
- 使用 TiUP 升级 TiDB
- What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
- ESP series pin description diagram summary
- 将 NFT 设置为 ENS 个人资料头像的分步指南
- Day29-t77 & t1726-2022-02-13-don't answer by yourself
猜你喜欢
On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced
[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]
Synchronized solves problems caused by sharing
【MySQL】锁
[brush questions] top101 must be brushed in the interview of niuke.com
Yyds dry goods inventory three JS source code interpretation eventdispatcher
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
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
Analysis of pointer and array written test questions
NFT smart contract release, blind box, public offering technology practice -- contract
随机推荐
Migrate data from a tidb cluster to another tidb cluster
Understanding of law of large numbers and central limit theorem
VMware 虚拟化集群
让学指针变得更简单(三)
Résumé des diagrammes de description des broches de la série ESP
从表中名称映射关系修改视频名称
Nft智能合约发行,盲盒,公开发售技术实战--合约篇
2022.02.13 - NC003. Design LRU cache structure
1202 character lookup
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
"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
备份与恢复 CR 介绍
【MySQL】日志
从 SQL 文件迁移数据到 TiDB
"Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
LDAP应用篇(4)Jenkins接入
Analysis of Top1 accuracy and top5 accuracy examples
Migrate data from SQL files to tidb
Vocabulary notes for postgraduate entrance examination (3)
IOT -- interpreting the four tier architecture of the Internet of things