JavaBaby
JavaBaby

Reputation: 33

Why the EntityManager is closed?

I'm developing a small stock control application using Java-SE8, JPA and Hibernate and this is my first contact with ORMs and the Persistence API. All was fine, but my tests failed when I created a method to retrieve things on the database(MySQL) and I get an:

"IllegalStateException: Entity Manager is closed."

I really don't know what is happening and spent almost two days trying to fix this. Can anyone help me, please?

Here are the pieces of code that trigger the error.

DAO.java

private EntityManager getEntityManager() {
    EntityManagerFactory factory = null;
    EntityManager entityManager = null;

    try {
      factory = Persistence.createEntityManagerFactory("stockcontrol");
      entityManager = factory.createEntityManager();
    } finally {
      factory.close();
    }

    return entityManager;
}

public <T> List<T> findAllByClass(Class clazz) {
    EntityManager manager = null;

    try{
        manager = getEntityManager();
        String hql = "FROM " + clazz.getName();
        Query hqlQuery = manager.createQuery(hql);
        return hqlQuery.getResultList();
    } finally {
        manager.close();
    }
}

Moneytory.java

DAO dao = new DAO();

public static void registerProduct(String name, int amount, double unitPrice,
        String strCategory) throws RegisterException {

    List<Product> products = dao.findAllByClass(Product.class);
    List<Category> categories = dao.findAllByClass(Category.class);

    if(products.contains(getProductByName(name)))
        throw new RegisterException("The product already exists");


    Product newProduct = null;
    Category newCategory = null;
    try{
        newCategory = new Category(strCategory);
        newProduct = new Product(name, amount, unitPrice, newCategory);
    } catch(IllegalArgumentException e){
        throw new RegisterException(e.getMessage());
    }

    if(!categories.contains(newCategory))
        dao.persist(newCategory);

    dao.persist(newProduct);    
    dao.flush();

}

And the StackTrace I've got:

java.lang.IllegalStateException: EntityManager is closed
    at org.hibernate.jpa.internal.EntityManagerImpl.checkOpen(EntityManagerImpl.java:105)
    at org.hibernate.jpa.internal.EntityManagerImpl.checkOpen(EntityManagerImpl.java:96)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:326)
    at br.edu.noorg.moneytory.dao.DAO.findAllByClass(DAO.java:154)
    at br.edu.noorg.moneytory.core.MoneyTory.registerProduct(MoneyTory.java:111)
    at br.edu.noorg.moneytory.test.MoneyToryTest.mustRegisterProduct(MoneyToryTest.java:92)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Upvotes: 3

Views: 29104

Answers (1)

Neil Stockton
Neil Stockton

Reputation: 11531

So you created an EntityManagerFactory (EMF), then an EntityManager (from the EMF), and then closed the EntityManagerFactory… which will close all of the EntityManager that it owns. Don't close the EMF!!

Upvotes: 3

Related Questions