4

When we use the @PersistenceContext annotation on an EntityManager in a JAVA EE environment, the container will create the entityManagerFactory (one for the whole session i guess) and will create a new EntityManager for each request ( by proxying it).

But using CDI without a JAVA EE container i saw something like this:

public class EntityManagerProducer {
private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("livraria");

    @Produces  
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void close(EntityManager em) {
    em.close();
}

}

Using that approach with CDI, will have the same effect and performance? Thanks in advance for any help

Anish B.
  • 7,321
  • 3
  • 12
  • 33
  • *"will have the same effect and performance"* as compared to what exactly on which environment exactly? This question is too ambiguous to answer. – BalusC Mar 27 '19 at 13:34
  • @BalusC My interest is regarding the em Scope, is the behavior exactly the same between the 2 injection approaches – Gab Mar 27 '19 at 15:00
  • That depends on environment as well. EE and SE are absolutely not the same as EE usually also infers a transaction context on it. This cannot be fully explained in a single sentence. The question about "performance" as formulated in its current form therefore also very confusing and ambiguous. What exactly is A and what exactly is B that you wanted to compare? Right now the ambiguity extends further than just A and B. – BalusC Mar 27 '19 at 15:06
  • @BalusC Ok sry i was asking a question and found that it was a duplicate of this one but it's not. My interest is in a EE context If you take the 1st example of https://stackoverflow.com/a/16534957/2087640, is the container still able to infer the transaction context when using the producer method approach. Thx for your time – Gab Mar 27 '19 at 15:43
  • @BalusC if you're able to word an acceptable answer to the OP question answering also my matter I'll be happy to award you the bounty – Gab Mar 27 '19 at 16:01

2 Answers2

3

No, the effect is not the same. Like Gab said in a previous answer, @PersistenceContext by default injects a separate entity manager per transaction. There is also an option to use an EXTENDED persistence context, but its beyond the scope of your question.

With the code that you provided using CDI you would get an instance per injection point which is not the same unless you create the injecting bean for each transaction (most likely not).

However, what you can do is to use a RequestScoped producer for your entity manager, like so:

 public class EntityManagerProducer {
     private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("livraria");

     @Produces
     @RequestScoped
     public EntityManager getEntityManager() {
         return emf.createEntityManager();
     }
 }

This will give you a similar semantics in a web application, but there may be some pitfalls if you plan to mix EJB and non-EJB code, since this entity manager is not aware of the transactions in progress. Some of this is covered here.

You should also check this similar question Getting a reference to EntityManager in Java EE applications using CDI where you can find a lot of excellent discussion on the matter.

Nestor Sokil
  • 1,957
  • 9
  • 25
2

"Is using that approach with CDI will have the same effect"

No it won't, using @PersistenceContext the java EE container will provide you a different entityManager instance for each transaction.

Here you will retrieve an unique instance at each injection point which can be problematic (em is not thread safe, moreover the persistence context state will not be consistent with the database one if your data are updated somewhere else since the related entity have been loaded).

Gab
  • 7,071
  • 2
  • 32
  • 62
  • I would also like to add that you can do some additional coding to inject a `RequestScoped` entity manager via CDI. This was discussed in this SO topic: https://stackoverflow.com/questions/19431423/getting-a-reference-to-entitymanager-in-java-ee-applications-using-cdi – Nestor Sokil Mar 30 '19 at 07:53
  • Ok, that's exactly my matter. thx a lot. @NestorSokil If you don't mind try to word a short answer covering both OP concern and mine and you'll get the bounty – Gab Apr 01 '19 at 09:29