Is Java EE container automatically closed by EntityManagerFactory?

Say I have this code in @Stateless Enterprise JavaBean:

@PersistenceUnit
EntityManagerFactory emf;

Will the Java EE container automatically close mine EntityManagerFactorysooner or later?

What the specs say

The Java Persistence specification, version 2.1, reads the following on page 338:

When the application has finished using the factory object manager, and / or when the application terminates, the application should close the factory object manager.

Good, so it's pretty straight forward. I need to close the factory.

However, since I introduced factory first, I'm a little nervous when I call the close () method myself. What if the container is not smarter than reusing a closed factory in the next bean that the factory wants? In addition, I do not like to add a lot of plumbing to connect my own logic to what happens or not when the application shuts down. Moreover, it just smells bad that I did not open / did not create a factory, but my code is responsible for closing it. Surely a massive bunch of Java EE specs can tell me about this issue?

.. . , JPA 2.1 EJB 3.2, factory, . , factory Persistence.createEntityManagerFactory() (. JPA 2.1, 345 355).

, , JPA 2.1 356:

[..] , PersistenceProvider.createContainerEntityManagerFactory factory EntityManagerFactory.close factory ( ).

, factory. , , , / , .

, " " EJB 3.2, . 286. , factory - .

, , , , , . , factory. , , JPA, , factory!

? - ?

+4
1

EntityManagerFactory , ( ). EMF, , . JPA 2.1 spec 9.1:

[...]

EntityManagerFactory , . EntityManagerFactory . EntityManager factory.

, , EntityManager, EMF.

+1

Source: https://habr.com/ru/post/1529582/


All Articles