Код: Выделить всё
@Entity
class MyEntity{
@Id
long id;
@Version
Long version;
}
class A {
private B bService;
@Transactional(readOnly = true)
public void execute() {
try {
bService.begin(10);
} catch (OptimisticLockingFailureException e) {
log.... //this is never catch at spring 3.3
}
}
class B {
@Transactional(propagation = Propagation.REQUIRES_NEW)
public MyEntity begin(Long id) {
MyEntity freshMyEntity = repository.findAwaiting(id)
.orElseThrow(EntityNotFoundException::new);
// at this moment another transaction commit entity with the same ID
// or when I pause this and commit it manualy in database
// both case increased version field
return repository.save(freshMyEntity);
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... ransaction