当前位置:网站首页>数据库 事务
数据库 事务
2022-06-30 10:54:00 【TYUT ljk】
事务
基本介绍
- 概念:如果一个包含多个步骤的业务操作,被事务管理,那么这些操作要么同时成功,要么同时失败
- 操作
(1)开启事务:start transaction
(2)回滚:rollback
(3)提交:commit - 事务提交方式
(1)自动提交
mysql就是自动提交的
一条DML(增删改)语句会自动提交一次事务
(2)手懂提交
Oracle默认手动提交
需要先开启事务,再提交
(3)修改默认提交方式
查看事务默认提交方式:SELECT @@autocommit
1 表示自动提交,0表示手动提交
修改默认提交方式:SET @@autocommit = 0
此时修改操作在不执行提交操作时,只是临时有效,不会影响实际的数据库
四大特征
- 原子性:是可分割的最小单位,要么同时成功,要么同时失败
- 持久性:当事务提交或回滚后,数据库会持久化的保存数据
- 隔离性:多个事务之间相互独立
- 一致性:事务操作前后,数据总量不变
事务的隔离级别
- 概念:多个事务之间相互独立,但是如果多个事务操作同一批数据,则会引发一些问题,设置不同的隔离级别就可以解决这些问题
- 存在问题:
(1)脏读:一个事务读取到另一个事务中还没有提交的数据
(2)不可重复读(虚读):在同一个事务中,两次读取到的数据不一样
(3)幻读:一个事务操作(DML)数据表中所有记录,另一个事务添加一条数据,则第一个事务查询不到自己的修改 - 隔离级别
(1)read uncommitted:读未提交
产生问题:脏读,不可重复读,幻读
(2)read committed:读已提交(Oracle默认)
产生问题:不可重复读,幻读
(3)repeatable read:可重复读(Mysql默认)
产生问题:幻读
(4)serializable:串行化
可以解决所有问题 - 注意:隔离级别从小到大,安全性越来越高,但是效率越来越低
- 查询隔离级别
select @@tx_isolation - 设置隔离级别
set global transaction isolation level 级别字符串
JDBC管理事务
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JDBCDemo {
public static void main(String[] args) throws SQLException {
Connection conn = null;
try{
conn = JDBCUtils.getConnect();
//开启事务
conn.setAutoCommit(false);
//sql执行
//提交事务
conn.commit();
} catch (Exception e) {
e.printStackTrace();
//发生异常,回滚
conn.rollback();
}finally {
//关闭资源
}
}
}
边栏推荐
- 100 important knowledge points that SQL must master: creating and manipulating tables
- 国产数据库的黄金周期要来了吗?
- 脚本中如何'优雅'避免MySQL登录提示信息
- 第一届中国数字藏品大会即将召开
- 什么是微信小程序,带你推开小程序的大门
- The first China Digital Collection conference will be held soon
- The life, working principle and application of electrochemical oxygen sensor
- [untitled]
- Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change
- SQL必需掌握的100个重要知识点:使用视图
猜你喜欢

从开源项目探讨“FPGA挖矿”的本质

Kongsong (ICT Institute) - cloud security capacity building and trend in the digital age

The life, working principle and application of electrochemical oxygen sensor

20万奖金池!【阿里安全 × ICDM 2022】大规模电商图上的风险商品检测赛火热报名中!...

Time complexity and space complexity

暑假学习记录

List introduction
![[机缘参悟-34]:光锥之内皆命运](/img/3e/9f5630ba382df7f7ce00705445cef8.jpg)
[机缘参悟-34]:光锥之内皆命运

Wechat Emoji is written into the judgment, and every Emoji you send may become evidence in court

高通发布物联网案例集 “魔镜”、数字农业已经成为现实
随机推荐
MCU firmware packaging Script Software
再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大
Livedata source code appreciation III - frequently asked questions
The life, working principle and application of electrochemical oxygen sensor
基于HAL库的LED驱动库
Flutter 从零开始 008 表单
20万奖金池!【阿里安全 × ICDM 2022】大规模电商图上的风险商品检测赛火热报名中!...
LVGL8.2 Simple Checkboxes
ESP32-C3入门教程 基础篇⑫——量产烧写设备配置和序列号, NVS partition分区确认, NVS 分区生成程序, csv转bin
Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change
List introduction
ArrayList and sequence table
100 important knowledge points that SQL must master: creating and manipulating tables
100 important knowledge points that SQL must master: updating and deleting data
训练一个图像分类器demo in PyTorch【学习笔记】
LeetCode Algorithm 86. Separate linked list
国产数据库的黄金周期要来了吗?
100 important knowledge points that SQL must master: insert data
100 important knowledge points that SQL must master: using stored procedures
Introduction to game theory