当前位置:网站首页>[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(); //主动回滚
}
}
}
边栏推荐
- IoT solution
- IDEA2021.2安装与配置(持续更新)
- 使用pyqt弹出消息提示框
- GM8775C MIPI转LVDS调试心得分享
- VCA821可变增益放大器
- Typora use
- Case | industrial iot solutions, steel mills high-performance security for wisdom
- GM7150,振芯科技,视频解码器,CVBS转BT656/601,QFN32,替换TVP5150/CJC5150
- Compatible with C51 and STM32 Keil5 installation method
- Industry where edge gateway strong?
猜你喜欢
随机推荐
剑指Offer 33.二叉搜索树的后序遍历序列
【LeetCode】合并
联阳IT66121FN提供SDI转HDMI方案分享
读取FBX文件踩坑清单
剑指Offer 36.二叉搜索树与双向链表 中序遍历
滑动窗口方法
AD Actual Combat
HDMI转MIPI CSI东芝转换芯片-TC358743XBG/TC358749XBG
【plang 1.4.3】定时器的使用
进程(番外):自定义shell命令行解释器
【LeetCode】链表相加 进位
【plang 1.4.6】Plang高级编程语言(发布)
剑指Offer 32.Ⅰ从上到下打印二叉树
开源代码交叉编译操作流程及遇到的问题解决(lightdm)
发布全新的配置格式 - AT
进程(下):进程控制、终止、等待、替换
AD8307对数检波器
Case | industrial iot solutions, steel mills high-performance security for wisdom
408-二叉树-先序中序后序层次遍历
uniCloud use









