springspring-data-jpaentitymanager

Spring data jpa entity manager scope


does spring data JPA bound entity manager to the current thread or the transaction? Say I have a method annotated with @Transactional that is called all the time. Does spring uses the entity manager from previous calls or create a new one every time? what happens to the entity manager after the transaction is committed?


Solution

  • EntityManager(s) are bound to @Transactional(transaction) and not to threads. When the transaction starts, Spring assigns distinct entity manager and on transaction rollback or commit, EntityManager instance life ends then. In the single thread, you can start multiple transaction and each transaction will have its own EntityManager.