Skip to content

KvApril/mysqlLockTest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

###使用排他锁

User: id,name,age,version

操作放在同一个事务中

//获取对象
1.User user = userService.queryUserByLock(id); 
  (select * from user where id=#{id} for update)
  
//获取age并进行操作
2.age = user.getAge()+1;


//更新数据库
3.userService.updateAge(id,age);
  (update user set age=#{age} where id=#{id})

###使用共享锁

//获取对象
1.User user = userService.queryUser(id); 
	(select * from user where id=#{id})

//获取age并进行操作
2.age = user.getAge()+1;

//更新数据库
3.userService.updateAgeByVersion(id,age);
  (update user set age=#{age},version = version + 1 where id=#{id} and version=#{version})

About

mysql的排它锁和共享锁测试

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages