在写入数据库的时候须要有锁,好比同时写入数据库的时候会出现丢数据,那么就须要锁机制。java
它们使用的场景以下:mysql
乐观锁适用于写少读多的情景,由于这种乐观锁至关于JAVA的CAS,因此多条数据同时过来的时候,不用等待,能够当即进行返回。web
悲观锁适用于写多读少的情景,这种状况也至关于JAVA的synchronized,reentrantLock等,大量数据过来的时候,只有一条数据能够被写入,其余的数据须要等待。执行完成后下一条数据能够继续。spring
他们实现的方式上有所不一样。sql
乐观锁采用版本号的方式,即当前版本号若是对应上了就能够写入数据,若是判断当前版本号不一致,那么就不会更新成功,好比数据库
update table set column = value
where version=${version} and otherKey = ${otherKey}复制代码
悲观锁实现的机制通常是在执行更新语句的时候采用for update方式,好比apache
update table set column='value' for update复制代码
这种状况where条件呢必定要涉及到数据库对应的索引字段,这样才会是行级锁,不然会是表锁,这样执行速度会变慢。springboot
下面我就弄一个spring boot(springboot 2.1.1 + mysql + lombok + aop + jpa)工程,而后逐渐的实现乐观锁和悲观锁。bash
假设有一个场景,有一个catalog商品目录表,而后还有一个browse浏览表,假如一个商品被浏览了,那么就须要记录下浏览的user是谁,而且记录访问的总数。app
表的结构很是简单:
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;复制代码
<?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,很是方便,有兴趣的同窗能够自行研究。
更新的时候将version字段传过来,而后更新的时候就能够进行version判断,若是version能够匹配上,那么就能够更新(方法:updateCatalogWithVersion)。
在实体类上的version字段上加入version,能够不用本身写SQL语句就能够它就能够自行的按照version匹配和更新,是否是很简单。
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);
}复制代码
自行写原生SQL,而后写上for update语句。(方法:findCatalogsForUpdate)
使用@Lock注解,而且设置值为LockModeType.PESSIMISTIC_WRITE便可表明行级锁。
还有我写的测试类,方便你们进行测试:
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();
}
}
}复制代码
乐观锁失败后会抛出ObjectOptimisticLockingFailureException,那么咱们就针对这块考虑一下重试,下面我就自定义了一个注解,用于作切面。
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次后就再也不重试。
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;
}
}复制代码