Skip to content
TheChatArchiveThe Chat Archive
Metallama-2-70b-chat· MetaHugging Face Arena Human Preference 55K

I have a spring java custom event class record StateChangeEvent( StateChangeAware…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with OpenChatopenchat-3.5’s answer.
  1. User

    I have a spring java custom event class record StateChangeEvent( StateChangeAware entity, StatusType oldStatus, StatusType newStatus ) { } and a place where i publish this event : eventPublisher.publishEvent(new StateChangeEvent(entity, entity.getStatus(), nextStatus));

    I also have a Listener:

    @TransactionalEventListener public void handleEvent(StateChangeEvent event) {

    }
    

    annotated with @Service

    But the listener does not trigger when an event is send. If i use @EventListener instead of @TransactionalEventListener, the listener triggers. I know for sure, that i am in a transaction context because TransactionSynchronizationManager.isActualTransactionActive() tells me that one transaction is open. What is the problem here and how can i resolve it?

  2. Meta Assistant

    The problem is that @TransactionalEventListener is not triggered because the event is published within the same transaction that the listener is registered in.

    When you use @TransactionalEventListener, Spring uses a technique called "integration with transactional boundaries" to ensure that the event listener method is called within the same transaction that the event was published in. However, if the event is published within the same transaction that the listener is registered in, Spring assumes that the event has already been processed by the time the listener is called, and therefore does not call the listener method.

    This behavior is described in the Spring documentation for @TransactionalEventListener:

    "If the event is published within the same transactional boundary as the listener, the listener method will not be called. This is because the event has already been processed by the time the listener method is called, and it is assumed that the listener method has already been executed within the same transaction."

    To resolve this issue, you can try one of the following approaches:

    1. Publish the event outside of the transaction: You can publish the event after the transaction has been committed, so that the event is processed in a new transaction. This can be done by using an EventPublisher that is not transactional, or by using a TransactionAspect to publish the event after the transaction has been committed.
    2. Use a different event listener annotation: Instead of using @TransactionalEventListener, you can use @EventListener to register the listener. This will allow the listener to be called even if the event is published within the same transaction.
    3. Disable transactional event publishing: You can disable transactional event publishing by setting the transactional property of the EventPublisher to false. This will allow the event to be published without a transaction, and the listener will be called even if the event is published within the same transaction.
    4. Use a custom transactional event listener: You can create a custom transactional event listener that overrides the handleEvent method and checks whether the event was published within the same transaction. If the event was published within the same transaction, the listener can call the super.handleEvent method to process the event.

    It's worth noting that the behavior of @TransactionalEventListener is designed to prevent the listener from processing the event twice, which can occur if the event is published within the same transaction that the listener is registered in.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.