Exception from sleep lock exclusion

I have a way like this:

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void doSomeWork(){
 Entity = entity = dao.loadEntity();
 // do some related work
 ...
 try {
  dao.saveEntity(entity);
 }
 catch(StaleObjectStateException sose){
  dao.flush(entity);
  doSomeWork();
 }
}

I expected that using the transaction REQUIRES_NEW and the recursion shown, the StaleObjectStateException exception would eventually be thrown, but that is not the case.

How can I recover from this exception?

0
source share
1 answer

It turns out there is a bit of “gatch”, which I forgot ...

From spring docs

( ) 'external' - . , "", , , , @Transactional!

, .

-, ...

@Autowired
private ApplicationContext applicationContext;

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void doSomeWork(){
 Entity = entity = dao.loadEntity();
 // do some related work
 ...
 try {
  dao.saveEntity(entity);
 }
 catch(StaleObjectStateException sose){
  dao.flush(entity);
  applicationContext.getBean(this.getClass()).doSomeWork();
 }
}
+1

Source: https://habr.com/ru/post/1526121/


All Articles