program using hibernate does not terminate

前端 未结 13 854
孤街浪徒
孤街浪徒 2020-12-25 10:14

I created a program using Hibernate.

The program reaches the main function end, nevertheless the program is running.

I wonder if it happens when Sessio

13条回答
  •  眼角桃花
    2020-12-25 10:47

    I am using hibenate 5.2.12 with sqlite 3.20.1, managing the connection manually. In my case the problem was that not only the entity manager had to be closed but also the entity manager factory.

    With these attributes:

    EntityManager entityManager;
    EntityTransaction entityTransaction;
    

    This snippet is used when opening the DB and starting a transaction:

    EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, map);
    entityManager = emf.createEntityManager(map);
    entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();
    

    This snipped is used to commit the transaction and close the DB:

    entityTransaction.commit();
    if ( entityManager.isOpen() ) {
        entityManager.close();
    }
    EntityManagerFactory emf = entityManager.getEntityManagerFactory();
    if ( emf.isOpen() ) {
        emf.close();
    }
    

    Now with emf.close(); my application terminates as is should be.

提交回复
热议问题