This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Optimistic locking

  • Pessimistic locking: The belief that data is insecure, subject to change, and locked at any time. Operation again.
  • Optimistic lock: Compared to pessimistic lock, optimistic lock task data is safe, generally there is no problem, no matter what to do to lock. If a conflict occurs, an error message is returned and the user decides.

Optimistic lock implementation

  • Gets the current when the record is fetchedversion
  • Take this with you when you updateversion
  • When you perform an update,set version = newVersion where version = oldVersion
  • ifversionIf not, update fails

Use optimistic Locks

  • Database incrementversionFields:
ALTER TABLE `table_name`  
ADD COLUMN `version` INT DEFAULT 1 COMMENT 'Optimistic lock';
Copy the code
  • Add fields to the entity class
@Version  
private Integer version;
Copy the code
  • Register the optimistic lock component
@Configuration  
@EnableTransactionManagement  
public class MyBatisPlusConfig {  
  
 /** * to configure mybatisPlus plugin **@returnThe interceptor * /  
 @Bean  
 public MybatisPlusInterceptor mybatisPlusInterceptor(a) {  
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();  
        // Add optimistic locking plugins
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());  
        returninterceptor; }}Copy the code
  • Test success
// The optimistic lock was successfully tested
@Test  
public void testOptimisticLocker(a) {  
	 //1. Obtain user information
	 User user = userMapper.selectById(1L);  
	 //2. Modify user information
	 user.setName("test");  
	 user.setAge(25);  
	 //3. Perform the update
	 userMapper.updateById(user);  
}
Copy the code
Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@74431b9c] was not registered For Synchronization Because Synchronization is Not Active 2021-11-07 18:41:52.417 INFO 21884 -- [main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... The 2021-11-07 18:41:52. 21884-792 the INFO [main] com. Zaxxer. Hikari. HikariDataSource: HikariPool-1 - Start completed. JDBC Connection [HikariProxyConnection@1729992636 wrapping com.mysql.cj.jdbc.ConnectionImpl@774f2992] will not be managed by Spring ==> Preparing: SELECT id,name,age,email,create_time,update_time,version FROM user WHERE id=? ==> Parameters: 1(Long) <== Columns: id, name, age, email, create_time, update_time, version <== Row: 1, zbc, 14, [email protected], null, 2021-11-06 22:28:53, 1 <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@74431b9c] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@74ce7fdf] was not registered for synchronization because synchronization is not active JDBC Connection [HikariProxyConnection@33779587 wrapping com.mysql.cj.jdbc.ConnectionImpl@774f2992] will not be managed by Spring ==> Preparing: UPDATE user SET name=?, age=?, email=?, update_time=?, version=? WHERE id=? AND version=? ==> Parameters: Test (String), 25(Integer), [email protected](String), 2021-11-07 18:41:52.93(Timestamp), 2(Integer), 1(Long), 1(Integer) <== Updates: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@74ce7fdf]Copy the code
  • Test to fail
// The optimistic lock test failed
@Test  
public void testOptimisticLocker2(a) {  
  
	 / / thread 1
	 User user = userMapper.selectById(1L);  
	 user.setName("test1");  

	 // Simulate thread 2, perform modification first
	 User user2 = userMapper.selectById(1L);  
	 user2.setName("test2");  
	 userMapper.updateById(user2);  

	 // If there is no optimistic lock, the value of thread 2 is overwritten
	 userMapper.updateById(user);  
}
Copy the code
Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@f2fb225] was not registered For synchronization Because Synchronization is Not Active 2021-11-07 18:46:35.982 INFO 13340 -- [main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... The 2021-11-07 18:46:36. 13340-457 the INFO [main] com. Zaxxer. Hikari. HikariDataSource: HikariPool-1 - Start completed. JDBC Connection [HikariProxyConnection@2001676690 wrapping com.mysql.cj.jdbc.ConnectionImpl@602298b] will not be managed by Spring ==> Preparing: SELECT id,name,age,email,create_time,update_time,version FROM user WHERE id=? ==> Parameters: 1(Long) <== Columns: id, name, age, email, create_time, update_time, version <== Row: 1, test, 25, [email protected], null, 2021-11-07 18:41:53, 2 <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@f2fb225] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@13908f9c] was not registered for synchronization because synchronization is not active JDBC Connection [HikariProxyConnection@849031967 wrapping com.mysql.cj.jdbc.ConnectionImpl@602298b] will not be managed by Spring ==> Preparing: SELECT id,name,age,email,create_time,update_time,version FROM user WHERE id=? ==> Parameters: 1(Long) <== Columns: id, name, age, email, create_time, update_time, version <== Row: 1, test, 25, [email protected], null, 2021-11-07 18:41:53, 2 <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@13908f9c] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@642c72cf] was not registered for synchronization because synchronization is not active JDBC Connection [HikariProxyConnection@966457052 wrapping com.mysql.cj.jdbc.ConnectionImpl@602298b] will not be managed by Spring ==> Preparing: UPDATE user SET name=?, age=?, email=?, update_time=?, version=? WHERE id=? AND version=? ==> Parameters: Test2 (String), 25(Integer), [email protected](String), 2021-11-07 18:46:36.63(Timestamp), 3(Integer), 1(Long), 2(Integer) <== Updates: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@642c72cf] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6aa6c17] was not registered for synchronization because synchronization is not active JDBC Connection [HikariProxyConnection@1196877260 wrapping com.mysql.cj.jdbc.ConnectionImpl@602298b] will not be managed by Spring ==> Preparing: UPDATE user SET name=?, age=?, email=?, update_time=?, version=? WHERE id=? AND version=? ==> Parameters: Test1 (String), 25(Integer), [email protected](String), 2021-11-07 18:46:36.68(Timestamp), 3(Integer), 1(Long), 2(Integer) <== Updates: 0 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6aa6c17]Copy the code

We found that the modification operation failed

id name age email create_time update_time
1 test2 25 [email protected] null The 2021-11-07 18:46:37