Skip to content

Exception thrown inside event handler cannot be caught #4

@drupalspring

Description

@drupalspring

In a service class , I publish an event synchronously using bus.post(event).now() , the corresponding event handler which is annotated with @handler executes successfully. However if I throw an Exception inside the event handler , the thrown exception cannot be caught and org.springframework.transaction.TransactionSystemException is caught instead.

Please refer the following codes for more detail :

@Service
@Transactional  
public class Service{

   public void someAction(){
        /**Do some business logic ***/
        bus.post(anEvent).now();
   }
}
@Service
@Transactional
public class SomeEventHandler{

    @Handler
    public void handleWithdrawnEvent(Event event) {
        /**The event handler code***/
        throws  new ApplicationException("Oops , some error happens");
    }
}

if service.someAction() executes , my custom ApplicationException cannot be caught .Instead the following exception is caught instead:

org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions