Persistence.createEntityManagerFactory initial start too slow

Viewed 30

I'm building a small JavaFX desktop app.

There are 2 separate dbs. One of them has 2 tables with One-to-many relationships.

I am using JPA with Hibernate and Apache Derby Embedded as database. I'm using the latest versions of all dependencies.

I deploy the app using 'mvn compile package' command using 'maven-shade-plugin'.

The problem that I have is that Persistence.createEntityManagerFactory needs more than 6 seconds for initial start from the fat-jar. (starting from IntelliJ it needs 2 seconds which is kinda acceptable)

This is my persistence.xml file:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
         http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
         version="2.1">

<persistence-unit name="tm_persistence" transaction-type="RESOURCE_LOCAL">
    <description>TimeMeasurement Hibernate EntityManager</description>
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.nick.timemeasurementfx.Model.TmHeader</class>
    <class>com.nick.timemeasurementfx.Model.TmDetail</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>

    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
        <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:derby:db/timemeasurement"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
        <property name="show_sql" value="true"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="root"/>
    </properties>
</persistence-unit>

<persistence-unit name="za_persistence" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.nick.timemeasurementfx.Model.Za</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>

    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
        <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:derby:db/zadata"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
        <property name="show_sql" value="true"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="root"/>
    </properties>
</persistence-unit>

This is how I create the EntityManager: EntityManager and EntityManagerFactory are from jakarta.persistence.

public class EntityManagerTM {
private static final EntityManagerFactory entityManagerFactory;

static {
    try {
        long start = (new Date()).getTime();
        entityManagerFactory = Persistence.createEntityManagerFactory(BaseConstants.TM_PERSISTENCE_NAME);
        long end = (new Date()).getTime();
        System.out.println("++ TM it took "+(end-start)+" to create EM Factory");
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static EntityManager getEntityManager() {
    long start = (new Date()).getTime();
    EntityManager em = entityManagerFactory.createEntityManager();
    long end = (new Date()).getTime();
    System.out.println("++ TM it took "+(end-start)+" to create entity manager");
    return em;
}

}

Is there a way that the creation of the EntityManagerFactory to be faster?

Can I create both EntityManagerFactory in a separate threads when the app starts? (I assume that while the app is loading both EntityManagerFactory will create too and then just to use them)

Nick

P.S. I tried a lot of different suggestions/solutions that I found in the net but nothing helped me reduce the time. I tried with H2 instead of Apache Derby but it was worse.

0 Answers
Related