-
Bug
-
Resolution: Done
-
Major
-
7.1.1.Final
-
Release Notes
-
I have an Seam2 EAR application (Blog example from Seam distribution) which almost always fails to deploy with
javax.naming.NameNotFoundException: Error looking up blogEntityManagerFactory, service service jboss.naming.context.java.blogEntityManagerFactory is not started
The application.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?> <application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.x sd" version="6"> <description>Examples for Seam 2 Framework in Java EE6 environment</description> <display-name>blog-ear</display-name> <initialize-in-order>true</initialize-in-order> <module> <ejb>blog-ejb.jar</ejb> </module> <module> <web> <web-uri>blog-web.war</web-uri> <context-root>/seam-blog</context-root> </web> </module> <module> <ejb>jboss-seam.jar</ejb> </module> <library-directory>lib</library-directory> </application>
The blog-ejb.jar contains persistence.xml:
<?xml version="1.0" encoding="UTF-8"?> <persistence 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_1_0.xsd" version="1.0"> <persistence-unit name="entityManager"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.cache.use_second_level_cache" value="true"/> <property name="hibernate.cache.use_query_cache" value="true"/> <property name="jboss.entity.manager.factory.jndi.name" value="java:/blogEntityManagerFactory"/> <property name="hibernate.search.default.directory_provider" value="filesystem"/> <property name="hibernate.search.default.indexBase" value="./blogindexes"/> </properties> </persistence-unit> </persistence>
The persistence unit is accessed in Seam via JNDI
(components.xml)
<persistence:managed-persistence-context name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/blogEntityManagerFactory"/>
This (usually) fails in the IndexerService, which is started automatically during servlet startup, as the java:/blogEntityManagerFactory is not yet ready.
Note that in the application.xml the "initialize-in-order" is defined, so the web module should not start before the blog-ejb.jar ( which contains the persistence.xml), is started.
Source of IndexerService:
package actions; import javax.ejb.Remove; import org.hibernate.search.jpa.FullTextEntityManager; import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.Create; import org.jboss.seam.annotations.Destroy; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; import org.jboss.seam.annotations.Startup; /** * Index Blog entry at startup * * @author Emmanuel Bernard * @author Sanne Grinovero */ @Name("indexerService") @Scope(ScopeType.APPLICATION) @Startup public class IndexerService { @In private FullTextEntityManager entityManager; @Create public void index() { entityManager.createIndexer().start(); } @Remove @Destroy public void stop() {} }
Also note that if I add the following bean to seam-ejb.jar it will correctly wait with the deployment of the web archive:
import javax.persistence.PersistenceUnit; import javax.persistence.EntityManagerFactory; import javax.ejb.Singleton; import javax.ejb.Startup; @Singleton @Startup public class Aargh { @PersistenceUnit private EntityManagerFactory emf; }
I believe this workaround shouldn't be necessary, and the initialize-in-order should also work with persistence units.
This issue affects all Seam2 EAR applications which use seam-managed entity manager in a @Startup bean, or any other legacy applications accessing persistence units through JNDI.