0

I have 4 transaction managers in my spring context. Based on some logic I want to invoke the respective transaction manager. How should I do ? For the following code I am getting an exception

java.lang.ClassCastException: org.hibernate.action.internal.DelayedPostInsertIdentifier cannot be cast to java.math.BigInteger

However, if I give a single @Transactional(value = "myservicetransactionManager", propagation = Propagation.REQUIRES_NEW) at the class level it works fine, but only for that scenario. Rest all fail.

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

}
Philip
  • 28
  • 4
  • 3
    This sounds very bizarre, first of all having 4 transaction managers and wanting to select one of them dynamically instead of having methods grouped by it. What's the big picture here? – Kayaman Jun 15 '15 at 11:48
  • Regardless of why you *(think)* you need it, you're not on the right track - due to how `@Transactional` and proxies in Spring work, calling the individual private methods from within the same class bypasses the proxied object, making the annotation effectively invisible to Spring. – kryger Jun 15 '15 at 11:59
  • Do you mean I should have one transaction per class, so in this case should I have 4 transactions? Moreover, I have tried making all members public. still it doesn't work – Philip Jun 15 '15 at 12:02
  • @Philip This is a fairly broad question in itself, this could be a good starting point: http://stackoverflow.com/q/2865055/1240557 – kryger Jun 15 '15 at 12:05
  • Thanks @kryger it worked. – Philip Jun 15 '15 at 14:26

0 Answers0