Recovering from hibernate optimistic locking exception

夙愿已清 提交于 2019-12-12 09:27:09

问题


I have a method 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 was expecting that by using REQUIRES_NEW transaction propagation and the recursion shown, the StaleObjectStateException would eventually clear but this isn't the case.

How do I recover from this exception?


回答1:


Turns out there's a bit of a 'gatcha' which I've overlooked...

From spring docs

In proxy mode (which is the default), only 'external' method calls coming in through the proxy will be intercepted. This means that 'self-invocation', i.e. a method within the target object calling some other method of the target object, won't lead to an actual transaction at runtime even if the invoked method is marked with @Transactional!

Because I've been recursing internally, the transaction demarcation wasn't getting applied.

The solution is to recurse via the proxy, like this...

@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();
 }
}


来源:https://stackoverflow.com/questions/12252535/recovering-from-hibernate-optimistic-locking-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!