在运行时设置@Transactional值

时间:2015-06-15 11:45:27

标签: java spring hibernate jpa transactions

我在春天的上下文中有4个事务管理器。根据一些逻辑,我想调用相应的事务管理器。我应该怎么做 ?对于以下代码,我得到一个例外

java.lang.ClassCastException:org.hibernate.action.internal.DelayedPostInsertIdentifier无法强制转换为java.math.BigInteger

但是,如果我在类级别提供一个 @Transactional(value =" myservicetransactionManager",propagation = Propagation.REQUIRES_NEW),它可以正常工作,但仅适用于该场景。休息都失败了。

@Service
public class MyServiceImpl implements MyService {

@Autowired
MyDao myDao;

@Override
public SomeResponse saveQueryResponseObject(SomeResponse someResponse) {
SomeResponse returnObject = null;

int logicValue = getLogicValue();
switch (logicValue) {
  case 0:
    returnObject = callforzero(someResponse);
    break;
  case 1:
    returnObject = callforfirst(someResponse);
    break;
  case 2:
    returnObject = callforsecond(someResponse);
    break;
  case 3:
    returnObject = callforthird(someResponse);
    break;
}
return returnObject;
}

@Transactional(value = "myservicetransactionManager", propagation = Propagation.REQUIRES_NEW)
private SomeResponse callforzero(SomeResponse someResponse) {
return myDao.saveResponse(someResponse);
}

@Transactional(value = "myservice1transactionManager", propagation = Propagation.REQUIRES_NEW)
private SomeResponse callforfirst(SomeResponse someResponse) {
return myDao.saveResponse(someResponse);
}

@Transactional(value = "myservice2transactionManager", propagation = Propagation.REQUIRES_NEW)
private SomeResponse callforsecond(SomeResponse someResponse) {
return myDao.saveResponse(someResponse);
}

@Transactional(value = "myservice3transactionManager", propagation = Propagation.REQUIRES_NEW)
private SomeResponse callforthird(SomeResponse someResponse) {
return myDao.saveResponse(someResponse);
}

}

0 个答案:

没有答案