Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting EntityManager Vs. EntityManagerFactory

A long question, please bear with me.

We are using Spring+JPA for a web application. My team is debating over injecting EntityManagerFactory in the GenericDAO (a DAO based on Generics something on the lines provided by APPFUSE, we do not use JpaDaosupport for some reason) over injecting an EntityManager. We are using "application managed persistence".

The arguments against injecting a EntityManagerFactory is that its too heavy and so is not required, the EntityManager does what we need. Also, as Spring would create a new instance of a DAO for every web request(I doubt this) there are not going to be any concurrency issues as in the same EntityManager instance is shared by two threads.

The argument for injecting EFM is that its a good practice over all its always good to have a handle to a factory.

I am not sure which is the best approach, can someone please enlighten me?

like image 768
SB. Avatar asked Aug 21 '09 04:08

SB.


People also ask

What is EntityManager and EntityManagerFactory in JPA?

Several entity manager factories can be prepared for connecting to different data stores. JPA EntityManager is used to access a database in a particular application. It is used to manage persistent entity instances, to find entities by their primary key identity, and to query over all entities.

Is EntityManagerFactory and EntityManager thread safe?

EntityManagerFactory instances are thread-safe. Applications create EntityManager instances in this case by using the createEntityManager method of javax.

What is the difference between EntityManagerFactory manager and SessionFactory?

Using EntityManagerFactory approach allows us to use callback method annotations like @PrePersist, @PostPersist,@PreUpdate with no extra configuration. Using similar callbacks while using SessionFactory will require extra efforts. Related Hibernate docs can be found here and here.


2 Answers

The pros and cons of injecting EntityManagerFactory vs EntityManager are all spelled out in the Spring docs here, I'm not sure if I can improve on that.

Saying that, there are some points in your question that should be cleared up.

...Spring would create a new instance of a DAO for every web request...

This is not correct. If your DAO is a Spring bean, then it's a singleton, unless you configure it otherwise via the scope attribute in the bean definition. Instantiating a DAO for every request would be crazy.

The argument for injecting EMF is that its a good practice over all its always good to have a handle to a factory.

This argument doesn't really hold water. General good practice says that an object should be injected with the minimum collaborators it needs to do its job.

like image 125
skaffman Avatar answered Sep 23 '22 23:09

skaffman


I am putting down what I have finally gathered. From the section "Implementing DAOs based on plain JPA" in the Spring Reference:

Although EntityManagerFactory instances are thread-safe, EntityManager instances are not. The injected JPA EntityManager behaves like an EntityManager fetched from an application server's JNDI environment, as defined by the JPA specification. It delegates all calls to the current transactional EntityManager, if any; otherwise, it falls back to a newly created EntityManager per operation, in effect making its usage thread-safe.

This means as per JPA specifications EntityManager instances are not thread safe, but if Spring handles them, they are made thread safe.

If you are using Spring, it is better to inject EntityManagers instead of EntityManagerFactory.

like image 29
SB. Avatar answered Sep 22 '22 23:09

SB.