LockModeType.OPTIMISTIC은 LockModeType.OPTIMISTIC_FORCE_INCREMENT가 작동하는 동안 작동하지 않습니다.LockModeType.OPTIMISTIC가 작동하지 않음
@Transactional
public void test(Integer task){
if (task == 1) {
em.find(Company.class, 1000059, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
}
if (task == 2) {
em.find(Company.class, 1000059, LockModeType.OPTIMISTIC);
System.out.println("waiting for task 1 ...");
Thread.sleep(10000); // time to call task1
}
}
나는 task2의 내부 작업 1을 호출하고 난 task2의 트랜잭션이 롤백 할 수 있지만 결국 둘 다 최선을 다하고 괜찮 기대합니다. 이것은 task2가 회사 버전에서 낙관적 인 잠금을 가지고 있고 task1이 중간에이를 증가시키기 때문에 발생하지 않아야합니다. 로깅 된 SQL은 트랜잭션이 끝날 때 버전을 확인하지만 롤백이 없음을 보여줍니다 (JpaOptimisticLockingFailureException).
Task2 :
select ... from company where company.id=1000059
작업 1 :
select ... from company where company.id=1000059
update Company set version=57 where id=1000059 and version=56
commit
Task2 :
select version from Company where id =1000059
commit
나는 코드가 잘 작동 할 OPTIMISTIC_FORCE_INCREMENT 낙관적 변경하고 외부 트랜잭션이 롤백 될 경우 JpaOptimisticLockingFailureException가 Throw됩니다. 그러나 이것은 읽기 전용 트랜잭션이 버전을 업데이트 할 필요가 있다는 것을 의미, 내가 트랜잭션 격리을 변경하여 해결 최대 절전 모드 4.3.0.Final 봄 4.0.0.RELEASE