Closing JPA Entity_Manage_Factory for persistence unit 'default'


As an experienced developer, encountering the message "Closing JPA Entity_Manage_Factory for persistence unit 'default'" typically indicates that the EntityManagerFactory associated with the JPA persistence unit named 'default' is being closed. This error mostly shown when the application shuts down or when the EntityManagerFactory is explicitly closed by the application.

In this post we will discuess the possible solution of this error. In my case i'm not encountering any errors when attempting to build, compile, and verify the project. However, when I try to run it using Maven, I start receiving the message: 'Closing JPA EntityManagerFactory for persistence unit 'default' HikariPool-1 - Shutdown completed.'"

1. EntityManagerFactory: The EntityManagerFactory is responsible for creating and managing EntityManager instances. It's a heavyweight object and should be created once per application lifecycle.

2. Persistence Unit: In JPA, a persistence unit represents a set of entity classes that are managed together by the EntityManagerFactory.

Let's consider a simple Java application with JPA:


import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Main {
    private static final String PERSISTENCE_UNIT_NAME = "default";

    public static void main(String[] args) {
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        
        // Perform operations with EntityManager
        
        entityManagerFactory.close(); // Closing EntityManagerFactory
    }
}
  

In this example, we create an EntityManagerFactory using the persistence unit named 'default'. After performing necessary operations with EntityManager, we explicitly close the EntityManagerFactory using the close() method.

Now, the output we've encountered, "Closing JPA Entity_Manage_Factory for persistence unit 'default'", is a log message indicating that the EntityManagerFactory associated with the 'default' persistence unit is being closed.


Sometimes this error occurs due to a conflict between the spring.boot.starter.tomcat and spring.boot.starter.web dependencies.You can try remove the spring.boot.starter.tomcat can be resolve the issue
How to exclude the spring-boot-starter-tomcat dependency when including 
spring-boot-starter-web. This exclusion can help resolve conflicts between the embedded Tomcat server provided by spring-boot-starter-tomcat and other servlet containers or dependencies.
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>                         <!-- Exclude specific dependencies -->
        <exclusion>                      <!-- Specify the dependency to be excluded -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

The error message "Stopping Closing JPA EntityManagerFactory shutdown" typically indicates an issue related to the shutdown process of the JPA EntityManagerFactory.

To ensure that the shutdown process for the EntityManagerFactory is handled correctly in your application you can also try below step.

1. Double-check the code responsible for shutting down the EntityManagerFactory and associated resources. Ensure that all necessary cleanup operations are performed properly.

4. Dependency Management: Check for any dependencies or conflicts between libraries or components that might interfere with the shutdown process. Ensure that all dependencies are properly managed and compatible with each other.

By following above guide you should be able to address the error "Stopping Closing JPA EntityManagerFactory shutdown" effectively.