5. Closing the EntityManagerFactory
Prev | Chapter 7. EntityManagerFactory | Next

## 5. Closing the EntityManagerFactory

    public boolean isOpen();
    public void close();

`EntityManagerFactory` instances are heavyweight objects. Each factory might maintain a metadata cache, object state cache, `EntityManager` pool, connection pool, and more. If your application no longer needs an `EntityManagerFactory`, you should close it to free these resources. When an `EntityManagerFactory` closes, all `EntityManager`s from that factory, and by extension all entities managed by those `EntityManager`s, become invalid. Attempting to close an `EntityManagerFactory` while one or more of its `EntityManager`s has an active transaction may result in an `IllegalStateException`.

Closing an `EntityManagerFactory` should not be taken lightly. It is much better to keep a factory open for a long period of time than to repeatedly create and close new factories. Thus, most applications will never close the factory, or only close it when the application is exiting. Only applications that require multiple factories with different configurations have an obvious reason to create and close multiple `EntityManagerFactory` instances. Once a factory is closed, all methods except `isOpen` throw an `IllegalStateException`.

Prev | Up | Next
4. Retrieving Properties Information | Home | 6. PersistenceUnitUtil