Search code examples
javajpajakarta-eeentitylisteners

Does EntitiyListener run in same Thread?


I have an Entity which is annotated with

@EntityListener(MyListener.class)

In the class MyListener, I have an Method annotated with

@PrePersist 

Inside this method, do i have the same thread which did the change on the Entitiy? Or can it be another thread? I have stored some information on the thread that do the change on the entity and i want to access this information in the listener. Thanks for your help!


Solution

  • Yes, it will always be the same thread. From the JPA spec (v2.2):

    An entity manager must not be shared among multiple concurrently executing threads, as the entity manager and persistence context are not required to be threadsafe.

    The entity manager is only used from a single thread (or you have a bug), so the same must apply to the entity.