Koray Tugay Koray Tugay - 3 months ago 13
Java Question

Why is Hibernate blocking Java process to exit?

And how do I avoid it?

This is the code I have:

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="testdb" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.connection.url" value="jdbc:h2:tcp://localhost:9092/~/h2dbs/testdb"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.connection.characterEncoding" value="utf-8"/>
<property name="hibernate.show_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>


PersistenceUtil.java

public class PersistenceUtil {

private static EntityManagerFactory entityManagerFactory;

static {
entityManagerFactory = Persistence.createEntityManagerFactory("testdb");
}

public static EntityManager getEntityManager() {
final EntityManager entityManager = entityManagerFactory.createEntityManager();
return entityManager;
}
}


and TestClass.java

public class TestClass {
public static void main(String[] args) {
final EntityManager em = PersistenceUtil.getEntityManager();
final EntityTransaction transaction = em.getTransaction();
transaction.begin();
final TestTable testTable = new TestTable();
testTable.setData("Sample Data");
em.merge(testTable);
transaction.commit();
em.close();
}
}


So when I run the main method in TestClass, I will see a row inserted in the DB with "Sample Data" fine, but the program will never exit, it will just hang..

I think Hibernate is creating a thread, but how do I avoid it?

Answer

try to close entityManagerFactory also ,in your PersistenceUtil just make additional method to close entityManagerFactory

 public class PersistenceUtil {

        private static EntityManagerFactory entityManagerFactory;

        static {
            entityManagerFactory = Persistence.createEntityManagerFactory("testdb");
        }

        public static EntityManager getEntityManager() {
            final EntityManager entityManager = entityManagerFactory.createEntityManager();
            return entityManager;
        }

    public static void closeEntityManagerFactory() {
           entityManagerFactory.close();
        }
    }




public class TestClass {
    public static void main(String[] args) {
        final EntityManager em = PersistenceUtil.getEntityManager();
        final EntityTransaction transaction = em.getTransaction();
        transaction.begin();
        final TestTable testTable = new TestTable();
        testTable.setData("Sample Data");
        em.merge(testTable);
        transaction.commit();
        em.close():
        PersistenceUtil.closeEntityManagerFactory()

    }
}
Comments