弹簧数据 - 启用乐观锁定
注意:我不需要关于乐观锁定的解释。这个问题是关于在使用乐观锁定时,似乎是什么特定的Spring Data行为。
从 jpa 规范中,每当实体具有带注释的字段时,都应在实体上自动启用乐观锁定。@Version
如果我在使用存储库的春季数据测试项目中执行此操作,则锁定似乎没有被激活。在执行不可重复读取测试时,会抛出事实上的 no(参见 JPA 规范第 93 页上的 P2)OptimisticLockException
但是,从spring文档中我看到,如果我们用注释单个方法,那么底层系统就会正确地抛出一个(然后被spring捕获并以稍微不同的形式向上传播堆栈)。@Lock(LockModeType.OPTIMISTIC)
OptimisticLockException
这是正常的还是我错过了什么?我们是否有义务对所有方法进行注释(或创建一个采用锁定的基本存储库实现)以使用spring数据启用乐观行为?
我在 spring boot 项目 1.4.5 版的上下文中使用 spring 数据。
测试:
public class OptimisticLockExceptionTest {
static class ReadWithSleepRunnable extends Thread {
private OptimisticLockExceptionService service;
private int id;
UserRepository userRepository;
public ReadWithSleepRunnable(OptimisticLockExceptionService service, int id, UserRepository userRepository) {
this.service = service;
this.id = id;
this.userRepository = userRepository;
}
@Override
public void run() {
this.service.readWithSleep(this.userRepository, this.id);
}
}
static class ModifyRunnable extends Thread {
private OptimisticLockExceptionService service;
private int id;
UserRepository userRepository;
public ModifyRunnable(OptimisticLockExceptionService service, int id, UserRepository userRepository) {
this.service = service;
this.id = id;
this.userRepository = userRepository;
}
@Override
public void run() {
this.service.modifyUser(this.userRepository, this.id);
}
}
@Inject
private OptimisticLockExceptionService service;
@Inject
private UserRepository userRepository;
private User u;
@Test(expected = ObjectOptimisticLockingFailureException.class)
public void thatOptimisticLockExceptionIsThrown() throws Exception {
this.u = new User("email", "p");
this.u = this.userRepository.save(this.u);
try {
Thread t1 = new ReadWithSleepRunnable(this.service, this.u.getId(), this.userRepository);
t1.start();
Thread.sleep(50);// To be sure the submitted thread starts
assertTrue(t1.isAlive());
Thread t2 = new ModifyRunnable(this.service, this.u.getId(), this.userRepository);
t2.start();
t2.join();
assertTrue(t1.isAlive());
t1.join();
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试服务:
@Component
public class OptimisticLockExceptionService {
@Transactional
public User readWithSleep(UserRepository userRepo, int id) {
System.err.println("started read");
User op = userRepo.findOne(id);
Thread.currentThread();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("read end");
return op;
}
@Transactional
public User modifyUser(UserRepository userRepo, int id) {
System.err.println("started modify");
User op = userRepo.findOne(id);
op.setPassword("p2");
System.err.println("modify end");
return userRepo.save(op);
}
}
存储库:
@Repository
public interface UserRepository extends CrudRepository<User, Integer> {
}