0

I get this error:

... 32 more
Caused by: java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory.

What I want to do is create a entity class User and a session Bean to access the User..

I get the error when I'm executing:

User result = null;
String q = "SELECT user FROM " + User.class.getName() + " user WHERE user.username LIKE :username";
Query query = entityManager.createQuery(q).setParameter("username", username);
List<User> users = query.getResultList();
if (users.size() > 0) {
    result = users.get(0);
    System.out.println(result);
}

persistence.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="DS3-ejbPU" transaction-type="JTA">
    <jta-data-source>db_ds_hw3</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>

EDIT 1:

injection code:

 @PersistenceContext(unitName = "DS3-ejbPU")
    private EntityManager em;
Corovei Andrei
  • 1,646
  • 6
  • 28
  • 42

2 Answers2

0

You are most probably running into a GlassFish bug in redeployment that has been fixed in version 4.0 (not out yet).

For the time being, as explained in the above link, the workaround is to undeploy - redeploy the application.


Related:

java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory

Community
  • 1
  • 1
perissf
  • 15,979
  • 14
  • 80
  • 117
  • I am using GlassFish yes. I tried to undeploy and redeploy and I get the same issue. – Corovei Andrei Nov 30 '12 at 11:08
  • Probably this happens because when the EMF is closed, it's too late, and you must restart GlassFish. The workaround works when the EMF is still alive. – perissf Nov 30 '12 at 12:40
0

I've faced with this bug too, reload domain helped me. In console enter:

asadmin restart-domain
Exterminator13
  • 2,152
  • 1
  • 23
  • 28