当前位置:网站首页>[Database] Four characteristics of transaction
[Database] Four characteristics of transaction
2022-08-02 05:03:00 【Money, Kun】
数据库事务
1.概念
A transaction is a series of rigorous operations in an application,Either all operations are performed successfully,要么全部执行失败.
2.事务的四大特性
- 原子性(Atomicity)
业务动作对应的SQL应该是一个整体,不可以再拆分,Modifications to data can either all succeed or all fail. - 一致性(Consistency)
The consistency of the data is reflected in two aspects:
①利用数据库的一些特性来保证部分一致性需求:比如声明某个列为NOT NULL 来拒绝NULL值得插入等.
②Most of them still need our programmers to guarantee when they write business code. - 隔离性(Isolation)
当有多个DBMS的用户,When adding, deleting, checking, and modifying data at the same time,Operations between users are relatively independent,One user's actions are invisible to other users. - 持久性(Durability)
一个事务一旦提交成功,Changes to data in the database are persistent.
3.如何使用事务
- 方式一 Databases use transactions
--开启事务
START TRANSACTION;
--执行sql的语句
INSERT INTO records(rid,bid) VALUES(1,2);
UPDATE books SET count=count-1 WHERE bid=2;
--rollback; //手动回滚
--提交事务
COMMIT; //代表一个事务的结束
注意点:
1.事务开启后,一旦执行SQL语句出现错误,All operations in the transaction will be rolled back to before the data operation;
2.Data is not submitted before,All operations on the database are not written to disk,Once some error occurs,The data will be restored to the state it was in before the operation;
3.When the database uses transactions,提交事务后,If something happensSQL的执行错误,The system will automatically roll back;
- 方式二 通过JDBC使用事务
1.Create a tool class for connecting to the database
package com.qk.utils;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class DbUtils {
private static final DataSource DATA_SOURCE;
static {
MysqlDataSource db=new MysqlDataSource();
String url="jdbc:mysql://localhost:3306/beta?useSSL=false&characterEncodiing=utf-8&severTimezone=Asia/Shanghai";
db.setUrl(url);
db.setUser("root");
db.setPassword("787426");
DATA_SOURCE =db;
}
public static Connection getConnection() throws SQLException {
return DATA_SOURCE.getConnection();
}
}
2.测试事务提交
package com.qk;
import com.qk.utils.DbUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Demo1 {
public static void main(String[] args) throws SQLException {
String sql1="insert into records(rid,bid) values(1,2)";
String sql2="update books set count=count-1 where bid =2";
//同一个事务中,执行sql1和sql2,means must be in the same oneConnection中完成
try(Connection connection = DbUtils.getConnection()) {
//connection中有一个autocommit属性,默认情况下是开启(true)
//开启状态下,意味着每一条sqlare treated as one transaction
//要让sql1和sql2看作一个整体,Autocommit needs to be turned off,手动提交事务
connection.setAutoCommit(false);
try(PreparedStatement ps=connection.prepareStatement(sql1)) {
ps.executeUpdate();
}
try(PreparedStatement ps=connection.prepareStatement(sql2)) {
ps.executeUpdate();
}
//手动提交事务,The above data operations are actually executed,数据写入磁盘
connection.commit();
}
}
}
3.JDBCFour scenarios for transaction use
- 有事务,commit成功
demo1,事务成功提交. - 没有事务,被动失败(重启服务器)
demo2,没有执行事务,重启服务器后,sql2执行失败,Database data was not updated successfully
package com.qk;
import com.qk.utils.DbUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Demo2 {
public static void main(String[] args) throws SQLException {
String sql1="insert into records(rid,bid) values(1,2)";
String sql2="update books set count=count-1 where bid =2";
try(Connection connection = DbUtils.getConnection()) {
try(PreparedStatement ps=connection.prepareStatement(sql1)) {
ps.executeUpdate();
}
//Finish the first daysql1后,第二天sql2执行失败
try(PreparedStatement ps=connection.prepareStatement(sql2)) {
ps.executeUpdate();
}
}
}
}
- 有事务,被动失败(重启服务器)程序出错
demo3,开启事务,After restarting the server or a program error,The data will be rolled back
package com.qk;
import com.qk.utils.DbUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Demo3 {
public static void main(String[] args) throws SQLException {
String sql1="insert into records(rid,bid) values(1,2)";
String sql2="update books set count=count-1 where bid =2";
try(Connection connection = DbUtils.getConnection()) {
//开启事务
connection.setAutoCommit(false);
try(PreparedStatement ps=connection.prepareStatement(sql1)) {
ps.executeUpdate();
}
//Finish the first daysql1后,第二天sql2执行失败
try(PreparedStatement ps=connection.prepareStatement(sql2)) {
ps.executeUpdate();
}
connection.commit();
}
}
}
- 有事务,主动失败(rollback)
demo4,开启事务,执行完sql语句后,Actively roll back the transaction,All data operations will be rolled back to before the operation
package com.qk;
import com.qk.utils.DbUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Demo4 {
public static void main(String[] args) throws SQLException {
String sql1="insert into records(rid,bid) values(1,2)";
String sql2="update books set count=count-1 where bid =2";
try(Connection connection = DbUtils.getConnection()) {
//开启事务
connection.setAutoCommit(false);
try(PreparedStatement ps=connection.prepareStatement(sql1)) {
ps.executeUpdate();
}
try(PreparedStatement ps=connection.prepareStatement(sql2)) {
ps.executeUpdate();
}
connection.rollback(); //主动回滚
}
}
}
边栏推荐
猜你喜欢
LL(1)文法 :解决 if-else/if-else 产生式二义性问题
【网络基础】浏览器输入一个URL之后,都发生了什么(详细讲解)
bluez5.50蓝牙文件传输
Hash table problem solving method
剑指Offer 34.二叉树中和为某一值的路径 dfs+回溯
2020 - AAAI - Image Inpainting论文导读《Learning to Incorporate Structure Knowledge for Image Inpainting》
USB_ID介绍
开源日志库 [log4c] 使用
WebApp 在线编程成趋势:如何在 iPad、Matepad 上编程?
【LeetCode】合并
随机推荐
2020 - AAAI - Image Inpainting论文导读《Learning to Incorporate Structure Knowledge for Image Inpainting》
Chrome 里的小恐龙游戏是怎么做出来的?
ICN6211:MIPI DSI转RGB视频转换芯片方案介绍 看完涨知识了呢
R语言 —— 多元线性回归
剑指Offer 35.复杂链表的复制
Hash table problem solving method
全加器高进位和低进位的理解
AD Actual Combat
【详解】优先级队列的底层实现
蛮力法求解凸包问题
AD8307对数检波器
Kinematics Analysis of Robot Arm
【plang 1.4.4】编写茶几玛丽脚本
【LeetCode】求和
idea中创建jsp项目详细步骤
Host your own website with Vercel
【plang 1.4.6】Plang高级编程语言(发布)
倒排单词
【nRF24L01 connects with Arduino to realize wireless communication】
振芯GM7123C:功能RGB转VGA芯片方案简介