Reputation: 420
I can't find information about relationship between EntityManager and transactions in spring data jpa application.
Which one of the statements is correct:
Upvotes: 4
Views: 2283
Reputation: 1033
The correct answer is: one shared instance of EntityManager is used for all transactions in the same persistence context.
We can have in consideration two things here:
First, the definition in the EntityManager Interface
An EntityManager instance is associated with a persistence context. A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. Within the persistence context, the entity instances and their lifecycle are managed.
The set of entities that can be managed by a given EntityManager instance is defined by a persistence unit. A persistence unit defines the set of all classes that are related or grouped by the application, and which must be colocated in their mapping to a single database.
Second, the constructor of the Spring SimpleJpaRepository:
public SimpleJpaRepository(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
Assert.notNull(entityInformation, "JpaEntityInformation must not be null!");
Assert.notNull(entityManager, "EntityManager must not be null!");
this.entityInformation = entityInformation;
this.em = entityManager;
this.provider = PersistenceProvider.fromEntityManager(entityManager);
}
em
is an attribute defined in that class with the final
modifier:
private final EntityManager em;
And in the methods of the SimpleJpaRepository are made calls to that em
without create new instances of the EntityManager.
Upvotes: 1