2 Replies Latest reply on Feb 9, 2012 3:02 AM by twasyl

    EntityManager injection failed because of a closed EntityManagerFactory

    twasyl

      Hi all,

       

      I'm still trying to use Arquillian but have some issues. The problem is to inject an EntityManager using @PersistenceContext(unitName = "something") in my EJBs.

      The without the injection, everything is fine, but when I call a method of my EJB that uses my EntityManager, I got an exception: java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory.

       

      In my test file, I create a JavaArchive using my persistence.xml file (which is detected (I had some issues first with my file so I know that is good now )) using this code:

       

      {code:java} @Deployment() public static JavaArchive createTestArchive() {     

           return ShrinkWrap.create(JavaArchive.class, "test.jar")

                .addClasses(ConfigurationDAORemote.class, ConfigurationDAO.class, Configuration.class, ConfigurationException.class)

                .addAsManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))

                .addAsManifestResource("META-INF/persistence.xml", ArchivePaths.create("persistence.xml"));{code}

       

      I'm using the remote Glassfish 3.1.1 arquillian container.

       

      Do I missed something? (apparently yes)

      Tahnks

        • 1. Re: EntityManager injection failed because of a closed EntityManagerFactory
          aslak

          Heya

           

          What does your TestClass look like?

           

          -aslak-

          • 2. Re: EntityManager injection failed because of a closed EntityManagerFactory
            twasyl

            Hi,

             

            My TestClass is this one:

             

            {code:java}

            @RunWith(Arquillian.class)

            public class DaoTest {

               

                @EJB

                private ConfigurationDAORemote configurationDAO;

               

                @Deployment

                public static JavaArchive createTestArchive() {

                    return ShrinkWrap.create(JavaArchive.class, "test.jar")

                            .addClasses(ConfigurationDAORemote.class,

                                        ConfigurationDAO.class,

                                        Configuration.class,

                                        ConfigurationException.class)

                            .addAsManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))

                            .addAsManifestResource("META-INF/persistence.xml", ArchivePaths.create("persistence.xml"));

                }

               

                @Test

                public void test() {

                    Assert.assertNotNull(this.configurationDAO);

                   

                    Configuration<Boolean> cnf = this.configurationDAO.getConfiguration("LDAP_HOSTNAME");

                   

                    Assert.assertNotNull("The configuration is null", cnf);

                    Assert.assertFalse(cnf.getTypedValue());

                }

            }

            {code}

             

            My EJB (implementation side) looks like this (if it could help):

             

            {code:java}

            @Singleton

            @Startup

            public class ConfigurationDAO implements ConfigurationDAORemote {

             

             

                @PersistenceContext(unitName = "Poc-PU")

                private EntityManager em;

             

                @Override

                public Configuration getConfiguration(String paramName) {

                    Configuration config;

                    try {

                        config = this.em.find(Configuration.class, paramName);

                    } catch (Exception ex) {

                        Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);

                        config = null;

                    }

             

             

                    return config;

                }

            }

            {code}

             

            Many thanks.

            Thierry.