在寫入數據庫的時候需要有鎖,比如同時寫入數據庫的時候會出現丟數據,那么就需要鎖機制。
1. 樂觀鎖適用于寫少讀多的情景,因為這種樂觀鎖相當于JAVA的CAS,所以多條數據同時過來的時候,不用等待,可以立即進行返回。
2. 悲觀鎖適用于寫多讀少的情景,這種情況也相當于JAVA的synchronized,reentrantLock等,大量數據過來的時候,只有一條數據可以被寫入,其他的數據需要等待。執行完成后下一條數據可以繼續。
他們實現的方式上有所不同,樂觀鎖采用版本號的方式,即當前版本號如果對應上了就可以寫入數據,如果判斷當前版本號不一致,那么就不會更新成功,比如 update table set column = value where version=${version} and otherKey = ${otherKey}。悲觀鎖實現的機制一般是在執行更新語句的時候采用for update方式,比如 update table set column='value' for update。這種情況where條件呢一定要涉及到數據庫對應的索引字段,這樣才會是行級鎖,否則會是表鎖,這樣執行速度會變慢。
下面我就弄一個spring boot(springboot 2.1.1 + mysql + lombok + aop + jpa)工程,然后逐漸的實現樂觀鎖和悲觀鎖。假設有一個場景,有一個catalog商品目錄表,然后還有一個browse瀏覽表,假如一個商品被瀏覽了,那么就需要記錄下瀏覽的user是誰,并且記錄訪問的總數。
表的結構非常簡單:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
create table catalog ( id int (11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵' , name varchar (50) NOT NULL DEFAULT '' COMMENT '商品名稱' , browse_count int (11) NOT NULL DEFAULT 0 COMMENT '瀏覽數' , version int (11) NOT NULL DEFAULT 0 COMMENT '樂觀鎖,版本號' , PRIMARY KEY (id) ) ENGINE=INNODB DEFAULT CHARSET=utf8; CREATE table browse ( id int (11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵' , cata_id int (11) NOT NULL COMMENT '商品ID' , user varchar (50) NOT NULL DEFAULT '' COMMENT '' , create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '創建時間' , PRIMARY KEY (id) ) ENGINE=INNODB DEFAULT CHARSET=utf8; |
POM.XML的依賴如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
<? xml version = "1.0" encoding = "UTF-8" ?> < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >2.1.1.RELEASE</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < groupId >com.hqs</ groupId > < artifactId >dblock</ artifactId > < version >1.0-SNAPSHOT</ version > < name >dblock</ name > < description >Demo project for Spring Boot</ description > < properties > < java.version >1.8</ java.version > </ properties > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-devtools</ artifactId > < scope >runtime</ scope > </ dependency > < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > < scope >runtime</ scope > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-data-jpa</ artifactId > </ dependency > < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > </ dependency > < dependency > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > < optional >true</ optional > </ dependency > <!-- aop --> < dependency > < groupId >org.aspectj</ groupId > < artifactId >aspectjweaver</ artifactId > < version >1.8.4</ version > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > </ project > |
項目的結構如下:
介紹一下項目的結構的內容:
entity包: 實體類包。
repository包:數據庫repository
service包: 提供服務的service
controller包: 控制器寫入用于編寫requestMapping。相關請求的入口類
annotation包: 自定義注解,用于重試。
aspect包: 用于對自定義注解進行切面。
DblockApplication: springboot的啟動類。
DblockApplicationTests: 測試類。
咱們看一下核心代碼的實現,參考如下,使用dataJpa非常方便,集成了CrudRepository就可以實現簡單的CRUD,非常方便,有興趣的同學可以自行研究。
實現樂觀鎖的方式有兩種:
1. 更新的時候將version字段傳過來,然后更新的時候就可以進行version判斷,如果version可以匹配上,那么就可以更新(方法:updateCatalogWithVersion)。
2. 在實體類上的version字段上加入version,可以不用自己寫SQL語句就可以它就可以自行的按照version匹配和更新,是不是很簡單。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public interface CatalogRepository extends CrudRepository<Catalog, Long> { @Query (value = "select * from Catalog a where a.id = :id for update" , nativeQuery = true ) Optional<Catalog> findCatalogsForUpdate( @Param ( "id" ) Long id); @Lock (value = LockModeType.PESSIMISTIC_WRITE) //代表行級鎖 @Query ( "select a from Catalog a where a.id = :id" ) Optional<Catalog> findCatalogWithPessimisticLock( @Param ( "id" ) Long id); @Modifying (clearAutomatically = true ) //修改時需要帶上 @Query (value = "update Catalog set browse_count = :browseCount, version = version + 1 where id = :id " + "and version = :version" , nativeQuery = true ) int updateCatalogWithVersion( @Param ( "id" ) Long id, @Param ( "browseCount" ) Long browseCount, @Param ( "version" ) Long version); } |
實現悲觀鎖的時候也有兩種方式:
1. 自行寫原生SQL,然后寫上for update語句。(方法:findCatalogsForUpdate)
2. 使用@Lock注解,并且設置值為LockModeType.PESSIMISTIC_WRITE即可代表行級鎖。
還有我寫的測試類,方便大家進行測試:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package com.hqs.dblock; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @RunWith (SpringRunner. class ) @SpringBootTest (classes = DblockApplication. class , webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class DblockApplicationTests { @Autowired private TestRestTemplate testRestTemplate; @Test public void browseCatalogTest() { String url = "http://localhost:8888/catalog" ; for ( int i = 0 ; i < 100 ; i++) { final int num = i; new Thread(() -> { MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add( "catalogId" , "1" ); params.add( "user" , "user" + num); String result = testRestTemplate.postForObject(url, params, String. class ); System.out.println( "-------------" + result); } ).start(); } } @Test public void browseCatalogTestRetry() { String url = "http://localhost:8888/catalogRetry" ; for ( int i = 0 ; i < 100 ; i++) { final int num = i; new Thread(() -> { MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add( "catalogId" , "1" ); params.add( "user" , "user" + num); String result = testRestTemplate.postForObject(url, params, String. class ); System.out.println( "-------------" + result); } ).start(); } } } |
調用100次,即一個商品可以瀏覽一百次,采用悲觀鎖,catalog表的數據都是100,并且browse表也是100條記錄。采用樂觀鎖的時候,因為版本號的匹配關系,那么會有一些記錄丟失,但是這兩個表的數據是可以對應上的。
樂觀鎖失敗后會拋出ObjectOptimisticLockingFailureException,那么我們就針對這塊考慮一下重試,下面我就自定義了一個注解,用于做切面。
1
2
3
4
5
6
7
8
9
10
11
|
package com.hqs.dblock.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target (ElementType.METHOD) @Retention (RetentionPolicy.RUNTIME) public @interface RetryOnFailure { } |
針對注解進行切面,見如下代碼。我設置了最大重試次數5,然后超過5次后就不再重試。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package com.hqs.dblock.aspect; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.hibernate.StaleObjectStateException; import org.springframework.orm.ObjectOptimisticLockingFailureException; import org.springframework.stereotype.Component; @Slf4j @Aspect @Component public class RetryAspect { public static final int MAX_RETRY_TIMES = 5 ; //max retry times @Pointcut ( "@annotation(com.hqs.dblock.annotation.RetryOnFailure)" ) //self-defined pointcount for RetryOnFailure public void retryOnFailure(){} @Around ( "retryOnFailure()" ) //around can be execute before and after the point public Object doConcurrentOperation(ProceedingJoinPoint pjp) throws Throwable { int attempts = 0 ; do { attempts++; try { pjp.proceed(); } catch (Exception e) { if (e instanceof ObjectOptimisticLockingFailureException || e instanceof StaleObjectStateException) { log.info( "retrying....times:{}" , attempts); if (attempts > MAX_RETRY_TIMES) { log.info( "retry excceed the max times.." ); throw e; } } } } while (attempts < MAX_RETRY_TIMES); return null ; } } |
大致思路是這樣了,歡迎拍磚。
GIT代碼:https://github.com/stonehqs/dblock
到此這篇關于數據庫常用的兩把鎖之樂觀鎖和悲觀鎖的文章就介紹到這了,更多相關數據庫鎖 樂觀鎖 悲觀鎖內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/huangqingshi/p/10165409.html