当前位置:网站首页>[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(); //主动回滚
}
}
}
边栏推荐
猜你喜欢
随机推荐
功率计,物联网,智能插座电路设计【毕业设计】
C语言教程 - 制作单位转换器
滑动窗口方法
【plang 1.4.6】Plang高级编程语言(发布)
Application of electronic flow on business trip
振芯科技GM8285C:功能TTL转LVDS芯片简介
LL(1)文法 :解决 if-else/if-else 产生式二义性问题
字符串匹配(蛮力法+KMP)
机械臂运动学解析
MPU6050 accelerometer and gyroscope sensor is connected with the Arduino
Pylon CLI 低成本的本地环境管控工具应用实例
Comparative analysis of OneNET Studio and IoT Studio
MIPI解决方案 ICN6202:MIPI DSI转LVDS转换芯片
uniCloud use
【plang 1.4.4】编写茶几玛丽脚本
云服务器web项目部署详解
实现动态库(DLL)之间内存统一管理
IoT solution
2020 - AAAI - Image Inpainting论文导读《Learning to Incorporate Structure Knowledge for Image Inpainting》
STM32 CAN 介绍以及相关配置









