user2184293
user2184293

Reputation: 51

@TransactionalEventListener is never being called in Spring Framework 4.2

I am implementing @EventListener from Spring 4.2 and it works fine. When I try to implement a @TransactionalEventListener it never gets called.

The ApplicationListenerMethodTransactionalAdapter makes the check:

if(TransactionSynchronizationManager.isSynchronizationActive())

and it is always false so it skips running the event because it says it is not in a transaction.

The code for the event listener is simply:

@TransactionalEventListener()
public void handleTransactionalAddEvent(Event event)
{
    logger.info("Add Event: {}");
}

The code which publishes the event is as follows:

@Override
@Transactional
public Order addToOrder(String username, Long orderId)
{
    Order order = getOrder(orderId, username);

    publisher.publishEvent(new Event(order, Event.EventType.ADD));

    ... Code to do stuff to the order ...

    updateOrder(order);
    return order;
}

If I change the @TransactionEventListener to just be @EventListener, or I add the fallbackExecution attribute it will run fine, but with the @TransactionEventListener it never gets called.

Upvotes: 1

Views: 5305

Answers (2)

Ranga Reddy
Ranga Reddy

Reputation: 21

@TransactionalEventListener is used when its method should be within the transaction. If no transaction is running, the listener is not invoked at all since we can’t honor the required semantics.

Upvotes: 2

user2184293
user2184293

Reputation: 51

I found out that we had a custom ApplicationEventMulticaster which was conflicting with the transactional event listeners. I removed it and now it works fine.

Upvotes: 4

Related Questions