3

How can catch the cause of rollback exception when run the code below (here reason is unique constraint error ), I'm using Weblogic, Spring, Hibernate, JTA

<tx:annotation-driven transaction-manager="txManager" order="200" />
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
</bean>


@Service
public class OrderService extend BaseService<Order, Long>   {


@Transactional 
 public Long  save(Order modelEntity)  {
    try{
         super.save(modelEntity);
 }
 catch(Exception exp){
    ....
  }

 }
}
------------------------------
@Controller
@RequestMapping("/test/core/Order")
public class OrderController extends BaseController{

@Autowired(required = true)
private IOrderService iOrderService;



@RequestMapping(value = "/save", method = RequestMethod.POST)
@ResponseBody
public Long save(@RequestBody Order  modelEntity) {

    return iOrderService.save(modelEntity); 
 }
}

@Controller
public class BaseController {
@ExceptionHandler(Exception.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public ResponseEntity<String> handleUncaughtException(Exception ex,    WebRequest request, HttpServletResponse response) throws IOException {
        String logMessage = ex.toString() + " " + ex.getMessage();

    if (ex.getCause()!= null) {
        logMessage += ex.getCause().toString() + " " +   ex.getCause().getMessage();

    }
 return new ResponseEntity<String>(" ", responseHeaders,    HttpStatus.INTERNAL_SERVER_ERROR);
 }

enter image description here

[ERROR] org.springframework.transaction.UnexpectedRollbackException: JTA transaction unexpectedly rolled back (maybe due to a timeout); nested exception is weblogic.transaction.RollbackException: setRollbackOnly called on transaction JTA transaction unexpectedly rolled back (maybe due to a timeout); nested exception is weblogic.transaction.RollbackException: setRollbackOnly called on transactionweblogic.transaction.RollbackException: setRollbackOnly called on transaction setRollbackOnly called on transactionorg.springframework.transaction.UnexpectedRollbackException: JTA transaction unexpectedly rolled back (maybe due to a timeout); nested exception is weblogic.transaction.RollbackException: setRollbackOnly called on transaction

Vahid Alizadeh
  • 267
  • 2
  • 11
  • did u try `try catch`?? – Antoniossss Sep 22 '16 at 07:08
  • yes ,but the problem is when save method is called, hibernate return unique constraint exception when saving to Database but because the method is transnational UnexpectedRollbackException occurred. but i want to catch the cause of RollbackException that here is unique constraint exception – Vahid Alizadeh Sep 22 '16 at 07:20
  • So catch it in `try catch` – Antoniossss Sep 22 '16 at 07:23
  • @Transactional public Long Save(Order modelEntity) { try{ return super.save(modelEntity); } – Vahid Alizadeh Sep 22 '16 at 07:24
  • and `catch (UniqueConstraintException e)` and what is the problem? – Antoniossss Sep 22 '16 at 07:26
  • when using hibernate, the scenario is When save( ); is executed, hibernate session hasn't flushed the session (i.e., executes save sql) util super.save() is finished. When hibernate actually flushes and executes the save sql, that error occurs. i wont to catch that exception which causes transaction rollback. – Vahid Alizadeh Sep 22 '16 at 07:29
  • I must say that i dont understand what you are trying to achieve here. You want to catch particular exception but you dont want to catch it at the same time. Anyway, you can catch it like I told you and rethrow as well. I might have misunderstood your description at some point as well. – Antoniossss Sep 22 '16 at 07:32
  • please check this http://stackoverflow.com/questions/36759599/transaction-rollback-when-exception-in-spring – Vahid Alizadeh Sep 22 '16 at 07:33
  • http://docs.oracle.com/javaee/7/api/javax/transaction/Transactional.html Both spring and jta apic contains `noRollbacOn` property where u can specify to not to rollback on particular exception and catch it outside of transactional scope. – Antoniossss Sep 22 '16 at 07:36
  • You should rather edit your post and attach given code as nobody will read this mess you created in the comments. – Antoniossss Sep 22 '16 at 08:13
  • according to the code above we expect to catch exception in BaseController but cached exception is UnexpectedRollbackException and cause of exception is not uniqeConstraint error – Vahid Alizadeh Sep 22 '16 at 08:25
  • http://stackoverflow.com/questions/2007097/unexpectedrollbackexception-a-full-scenario-analysis – Antoniossss Sep 22 '16 at 08:39
  • for getting the root cause of an UnexpectedRollbackException you can use exception.getRootCause() or exception.getMostSpecificCause() to catch the underlying exceptions. – Sanket Barapatre Jul 26 '21 at 05:38

0 Answers0