当前位置:网站首页>5.事务管理
5.事务管理
2022-08-02 14:10:00 【鱼子酱:P】
1.事务特性
2.事务的隔离性
- 常见的并发异常
- 第一类丢失更新、第二类丢失更新。
- 脏读、不可重复读、幻读。
第一类丢失更新:某一个事务的回滚,导致另外一个事务已更新的数据丢失了。
第二类丢失更新:某一个事务的提交,导致另外一个事务已更新的数据丢失了。
脏读:某一个事务,读取了另外一个事务未提交的数据。
不可重复读:某一个事务,对同一个数据前后读取的结果不一致。 (查询一条数据结果不一致)
幻读:某一个事务,对同一个表前后查询到的行数不一致。(查询多条数据结果不一致)
- 常见的隔离级别
- Read Uncommitted:读取未提交的数据。
- Read Committed:读取已提交的数据。
- Repeatable Read:可重复读。
- Serializable:串行化。
3.实现机制
- 悲观锁(数据库)
- 共享锁(S锁)
事务A对某数据加了共享锁后,其他事务只能对该数据加共享锁,但不能加排他锁。 - 排他锁(X锁)
事务A对某数据加了排他锁后,其他事务对该数据既不能加共享锁,也不能加排他锁。
- 共享锁(S锁)
- 乐观锁(自定义)
- 版本号、时间戳等
在更新数据前,检查版本号是否发生变化。若变化则取消本次更新,否则就更新数据(版本号+1)。
- 版本号、时间戳等
4.Spring事务管理
无论底层什么数据库,它的api都是一套
- 声明式事务
- 通过XML配置,声明某方法的事务特征。(本项目用这个)
- 通过注解,声明某方法的事务特征。
- 编程式事务
- 通过 TransactionTemplate 管理事务,并通过它执行数据库的操作。
声明式事务
在 @Transactional 注解上定义隔离机制和传播机制。当save1()方法中出现错误后,整个方法回滚,插入数据库失败。
// REQUIRED: 支持当前事务(外部事务),如果不存在则创建新事务.
// REQUIRES_NEW: 创建一个新事务,并且暂停当前事务(外部事务).
// NESTED: 如果当前存在事务(外部事务),则嵌套在该事务中执行(独立的提交和回滚),否则就会REQUIRED一样.
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public Object save1() {
// 新增用户
User user = new User();
user.setUsername("alpha");
user.setSalt(CommunityUtil.generateUUID().substring(0, 5));
user.setPassword(CommunityUtil.md5("123" + user.getSalt()));
user.setEmail("[email protected]");
user.setHeaderUrl("http://image.nowcoder.com/head/99t.png");
user.setCreateTime(new Date());
userMapper.insertUser(user);
// 新增帖子
DiscussPost post = new DiscussPost();
post.setUserId(user.getId());
post.setTitle("Hello");
post.setContent("新人报道!");
post.setCreateTime(new Date());
discussPostMapper.insertDiscussPost(post);
Integer.valueOf("abc");
return "ok";
}
编程式事务
public Object save2() {
transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
return transactionTemplate.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus status) {
// 新增用户
User user = new User();
user.setUsername("beta");
user.setSalt(CommunityUtil.generateUUID().substring(0, 5));
user.setPassword(CommunityUtil.md5("123" + user.getSalt()));
user.setEmail("[email protected]");
user.setHeaderUrl("http://image.nowcoder.com/head/999t.png");
user.setCreateTime(new Date());
userMapper.insertUser(user);
// 新增帖子
DiscussPost post = new DiscussPost();
post.setUserId(user.getId());
post.setTitle("你好");
post.setContent("我是新人!");
post.setCreateTime(new Date());
discussPostMapper.insertDiscussPost(post);
Integer.valueOf("abc");
return "ok";
}
});
}
边栏推荐
- Binder机制(下篇)
- 2021-10-14
- Mysql connection error solution
- 7. How to add the Click to RecyclerView and LongClick events
- Lightweight AlphaPose
- Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
- DP1101兼容CC1101是SUB1GHz无线收发芯片应用于智能家居
- SQL的通用语法和使用说明(图文)
- Win11 computer off for a period of time without operating network how to solve
- 使用libcurl将Opencv Mat的图像上传到文件服务器,基于post请求和ftp协议两种方法
猜你喜欢
Do Windows 10 computers need antivirus software installed?
Summarize computer network super comprehensive test questions
GMP scheduling model of golang
Network Security Packet Capture
FP5139电池与适配器供电DC-DC隔离升降压电路反激电路电荷泵电路原理图
STM32LL库使用——SPI通信
推开机电的大门《电路》(一):电压,电流,参考方向
Win10 cannot directly use photo viewer to open the picture
pygame绘制弧线
Mysql连接错误解决
随机推荐
A clean start Windows 7?How to load only the basic service start Windows 7 system
LORA芯片ASR6505无线远距离传输8位MCU
总结计算机网络超全面试题
Bash shell位置参数
Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
二叉树创建之层次法入门详解
基于矩阵计算的线性回归分析方程中系数的估计
背包问题-动态规划-理论篇
Use tencent cloud builds a personal blog
pygame draw arc
Win11 system cannot find dll file how to fix
SQL的通用语法和使用说明(图文)
FP5139电池与适配器供电DC-DC隔离升降压电路反激电路电荷泵电路原理图
关于c语言的调试技巧
网络安全抓包
In-depth understanding of Golang's Map
FP7195大功率零压差全程无频闪调光DC-DC恒流芯片(兼容调光器:PWM调光,无极调光,0/1-10V调光)
Configure clangd for vscode
How to simulate 1/3 probability with coins, and arbitrary probability?
pygame绘制弧线