当前位置:网站首页>[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(); //主动回滚
}
}
}
边栏推荐
猜你喜欢
随机推荐
Process (in): process state, process address space
同时求最大值与最小值(看似简单却值得思考~)
C语言教程 - 制作单位转换器
剑指Offer 34.二叉树中和为某一值的路径 dfs+回溯
Host your own website with Vercel
剑指Offer 47.礼物的最大值 动态规划
读取FBX文件踩坑清单
使用pyqt弹出消息提示框
ICN6211:MIPI DSI转RGB视频转换芯片方案介绍 看完涨知识了呢
引擎开发日志:场景编辑器开发难点
Laptop charging problems
振芯科技GM8285C:功能TTL转LVDS芯片简介
【LeetCode】求和
联阳(ITE)IT66021FN:HDMI转RGB芯片 3D 资料
idea中创建jsp项目详细步骤
Altium Designer Basics
使用buildroot制作根文件系统(龙芯1B使用)
【操作系统】线程安全保护机制
MIPI解决方案 ICN6202:MIPI DSI转LVDS转换芯片
【nRF24L01 connects with Arduino to realize wireless communication】