Noorus Khan
Noorus Khan

Reputation: 1476

Spring Hibernate : @Transaction (REQUIRES_NEW) not catching exception

A.java

try{
for(i=1;i<=5;i++){
List<Integer> intList = new ArrayList();
bService.executeMethod(i,intList);
}
}catch(Exception e){
 logger.error("exception occoured {}",e);
}

B.java

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void executeMethod(int i, List<Integer> intList){
try{
.......dostuff;
}catch(Exception e){
strList.add(i)
}
}

if something goes wrong executeMethod() for any of the iteration, lets i=2 (If some hibernate exception is happening its not getting caught in catch block), still the code should get executed for rest of the iteration values i=3 to i=5.

What ever exeption will come should get caught in catch block(as using Excetion class), but its not happening. I dont have much idea why its happening, Could someone help me on this.

OptimisticLockException is being thrown and without executing of rest of the iteration its getting terminated

Upvotes: 1

Views: 1070

Answers (1)

Guillaume F.
Guillaume F.

Reputation: 6473

Are you sure you are not hitting a transaction exception? If something happens in doStuff, it may break the transaction, marking it as invalid. As soon as you leave executeMethod, Spring tries to commit the transaction, and fails, throwing another Exception (in the proxy).

You should always log Exceptions, should it be at the debug level. Also, if you want to catch the transactional exception, you will have to do it in the caller.

An Exception that is thrown through the proxy will properly rollback the transaction. Since you catch it and ignore it, the error is "hidden" to Spring.

Upvotes: 1

Related Questions