当前位置:网站首页>5. Transaction management
5. Transaction management
2022-08-02 15:32:00 【Caviar :P】
1.事务特性

2.事务的隔离性
- 常见的并发异常
- 第一类丢失更新、第二类丢失更新.
- 脏读、不可重复读、幻读.
第一类丢失更新:某一个事务的回滚,导致另外一个事务已更新的数据丢失了.

第二类丢失更新:某一个事务的提交,导致另外一个事务已更新的数据丢失了.

脏读:某一个事务,读取了另外一个事务未提交的数据.

不可重复读:某一个事务,对同一个数据前后读取的结果不一致. (The result of querying a piece of data is inconsistent)

幻读:某一个事务,对同一个表前后查询到的行数不一致.(The results of querying multiple pieces of data are inconsistent)

- 常见的隔离级别
- Read Uncommitted:读取未提交的数据.
- Read Committed:读取已提交的数据.
- Repeatable Read:可重复读.
- Serializable:串行化.

3.实现机制
- 悲观锁(数据库)
- 共享锁(S锁)
事务A对某数据加了共享锁后,其他事务只能对该数据加共享锁,但不能加排他锁. - 排他锁(X锁)
事务A对某数据加了排他锁后,其他事务对该数据既不能加共享锁,也不能加排他锁.
- 共享锁(S锁)
- 乐观锁(自定义)
- 版本号、时间戳等
在更新数据前,检查版本号是否发生变化.若变化则取消本次更新,否则就更新数据(版本号+1).
- 版本号、时间戳等
4.Spring事务管理
No matter what the underlying database is,它的api都是一套
- 声明式事务
- 通过XML配置,声明某方法的事务特征.(This project uses this)
- 通过注解,声明某方法的事务特征.
- 编程式事务
- 通过 TransactionTemplate 管理事务,并通过它执行数据库的操作.
声明式事务
在 @Transactional The isolation mechanism and propagation mechanism are defined on the annotation.当save1()After an error occurs in the method,The whole method rolls back,插入数据库失败.
// 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";
}
});
}边栏推荐
- Configure clangd for vscode
- win10怎么设置不睡眠熄屏?win10设置永不睡眠的方法
- Mysql连接错误解决
- Win10电脑需要安装杀毒软件吗?
- Win11 system cannot find dll file how to fix
- Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
- cmake配置libtorch报错Failed to compute shorthash for libnvrtc.so
- 二叉树创建之层次法入门详解
- 用U盘怎么重装Win7系统?如何使用u盘重装系统win7?
- Spark及相关生态组件安装配置——快速回忆
猜你喜欢

Win11 system cannot find dll file how to fix

How to reinstall Win7 system with U disk?How to reinstall win7 using u disk?

6.统一记录日志

What should I do if I install a solid-state drive in Win10 and still have obvious lags?

5.事务管理

How to set the win10 taskbar does not merge icons

推开机电的大门《电路》(二):功率计算与判断

TCP三次握手、四次挥手

What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer

A clean start Windows 7?How to load only the basic service start Windows 7 system
随机推荐
STM32LL库——USART中断接收不定长信息
二叉排序树与 set、map
GMP scheduling model of golang
实战美团Nuxt +Vue全家桶,服务端渲染,邮箱验证,passport鉴权服务,地图API引用,mongodb,redis等技术点
Spark及相关生态组件安装配置——快速回忆
Do Windows 10 computers need antivirus software installed?
软件测试基础知识(背)
Win10 computer can't read U disk?Don't recognize U disk how to solve?
DP1332E刷卡芯片支持NFC内置mcu智能楼宇/终端poss机/智能门锁
Win10上帝模式干嘛的?Win10怎么开启上帝模式?
Redis常见面试题
二叉树的遍历:递归法/ 迭代法/ 统一迭代法(强QAQ)
Win11没有本地用户和组怎么解决
MATLAB图形加标注的基本方法入门简介
Detailed introduction to drawing complex surfaces using the plot_surface command
How to simulate 1/3 probability with coins, and arbitrary probability?
3. User upload avatar
Use libcurl to upload the image of Opencv Mat to the file server, based on two methods of post request and ftp protocol
Use tencent cloud builds a personal blog
What should I do if the Win10 system sets the application identity to automatically prompt for access denied?