12 Replies Latest reply on Apr 16, 2013 10:06 PM by smarlow

    Application Managed EntityManager JBoss AS 7.1.1

    mbuamuh

      Hi,

       

      i am using an application managed Entity manager and get it using the following code:

       

      private static String persistenceUnitName="eccore-ejb";

       

      private EntityManager getEntityManager() {

      EntityManagerFactory entityManagerFactory = Persistence .createEntityManagerFactory(persistenceUnitName);

      EntityManager entityManager = entityManagerFactory .createEntityManager();

      return entityManager;

      }

       

       

      But i keep getting an exception that there is no persistent provider. However my server logs show that the default persistence provider org.hibernate.ejb.HibernatePersistence is being used for that Persistence Unit. Following is my persistence.xml content and the error message. Does anyone have an idea what i might be doing wrong or come across this and how did they solve it?

       

       

       

      persistence:persistence version="2.0" xmlns:persistence="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:persistence-unit name="eccore-ejb" transaction-type="JTA" 

       

      >

       

       

       

      <persistence:provider>org.hibernate.ejb.HibernatePersistence</persistence:provider 

       

      >

       

       

       

      <persistence:jta-data-source>java:jboss/datasources/ECPM_DEV</persistence:jta-data-source 

       

      >

       

       

       

      <persistence:jar-file>com.ec.eccore-ejb-0.0.1-SNAPSHOT</persistence:jar-file 

       

      >

       

       

       

      <persistence:class>com.ec.eccore.util.resourcebundle.model.entities.BasisLanguageEntity</persistence:class 

       

      >

       

       

       

      <persistence:class>com.ec.eccore.util.resourcebundle.model.entities.LanguageSourceEntity</persistence:class 

       

      >

       

       

       

      <persistence:class>com.ec.eccore.util.resourcebundle.model.entities.LanguageTargetEntity</persistence:class 

       

      >

       

       

       

      <persistence:shared-cache-mode>ENABLE_SELECTIVE</persistence:shared-cache-mode 

       

      >

       

       

       

      <persistence:properties 

       

      >

       

       

       

      <persistence:property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" 

       

      />

       

       

       

      <persistence:property name="hibernate.show_sql" value="true" 

       

      />

       

       

       

      <persistence:property name="hibernate.cache.provider_class" value="org.hibernate.cache.SingletonEhCacheProvider" 

       

      />

       

       

       

      <persistence:property name="hibernate.cache.use_second_level_cache" value="true" 

       

      />

       

       

       

      <persistence:property name="hibernate.cache.use_query_cache" value="true" 

       

      />

       

       

       

       

       

       

       

      <!-- The region factory property is the "new" property (for Hibernate 

      3.3 and above) -->

       

       

       

       

       

       

      <!--property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/ -->

       

       

       

      </persistence:properties 

       

      >

       

       

       

      </persistence:persistence-unit 

       

      > 

      </

       

       

       

       

       

      persistence:persistence 

      >

       

       

       

      Caused by:

      javax.persistence.PersistenceException 

      : No Persistence provider for EntityManager named eccore-ejb 

      at javax.persistence.Persistence.createEntityManagerFactory(

       

       

      Persistence.java:69 

      ) [hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final] 

      at javax.persistence.Persistence.createEntityManagerFactory(

       

       

      Persistence.java:47 

      ) [hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final] 

      at com.ec.eccore.util.security.ObjectAccessInfoVer3.getEntityManager(

       

       

      ObjectAccessInfoVer3.java:78 

      ) [com.ec.eccore-ejb-0.0.1-SNAPSHOT.jar:]

        • 1. Re: Application Managed EntityManager JBoss AS 7.1.1
          nickarls

          Do you bundle any persistence impls (hibernate or other) in your application?

          • 2. Re: Application Managed EntityManager JBoss AS 7.1.1
            mbuamuh

            Yes, i bundle them. I bundle the following libraries:

            ejb3-persistence-1.0.2.GA.jar

            hibernate-annotations-3.4.0.GA.jar

            hibernate-commoms-annotations-3.1.0.GA.jar

            hibernate-jpa-2.0-api-1.0.0.Final.jar

            hibernate-jpamodelgen-1.0.0.Final.jar

             

            Could that be the reason why its not working?

            • 3. Re: Application Managed EntityManager JBoss AS 7.1.1
              nickarls

              Try taking them out, they don't do any good there and might throw off the classloading when looking for stuff.

              • 4. Re: Application Managed EntityManager JBoss AS 7.1.1
                mbuamuh

                I have removed them, but i am still getting the same error message.

                • 5. Re: Application Managed EntityManager JBoss AS 7.1.1
                  nickarls

                  Take out the persistence provider info, it should default to hibernate...

                  • 6. Re: Application Managed EntityManager JBoss AS 7.1.1
                    mbuamuh

                    I removed it but it still didn't work.

                    • 7. Re: Application Managed EntityManager JBoss AS 7.1.1
                      nickarls

                      And the message doesn't come from the persistence unit in the jar-file?

                      • 8. Re: Application Managed EntityManager JBoss AS 7.1.1
                        mbuamuh

                        No. After debugging i saw it comes from org.hibernate.ejb.Ejb3Configuration.configure(...)

                        Following is the method:

                        public Ejb3Configuration configure(String persistenceUnitName, Map integration) {
                                try {
                                    LOG.debugf("Look up for persistence unit: %s", persistenceUnitName);
                                    integration = integration == null ?
                                            CollectionHelper.EMPTY_MAP :
                                            Collections.unmodifiableMap( integration );
                                    Enumeration<URL> xmls = Thread.currentThread()
                                            .getContextClassLoader()
                                            .getResources( "META-INF/persistence.xml" );
                                    if (!xmls.hasMoreElements()) LOG.unableToFindPersistenceXmlInClasspath();
                                    while ( xmls.hasMoreElements() ) {
                                        URL url = xmls.nextElement();
                                        LOG.trace("Analyzing persistence.xml: " + url);
                                        List<PersistenceMetadata> metadataFiles = PersistenceXmlLoader.deploy(
                                                url,
                                                integration,
                                                cfg.getEntityResolver(),
                                                PersistenceUnitTransactionType.RESOURCE_LOCAL );
                                        for ( PersistenceMetadata metadata : metadataFiles ) {
                                            LOG.trace(metadata);

                         

                                            if ( metadata.getProvider() == null || IMPLEMENTATION_NAME.equalsIgnoreCase(
                                                    metadata.getProvider()
                                            ) ) {
                                                //correct provider

                         

                                                //lazy load the scanner to avoid unnecessary IOExceptions
                                                Scanner scanner = null;
                                                URL jarURL = null;
                                                if ( metadata.getName() == null ) {
                                                    scanner = buildScanner( metadata.getProps(), integration );
                                                    jarURL = JarVisitorFactory.getJarURLFromURLEntry( url, "/META-INF/persistence.xml" );
                                                    metadata.setName( scanner.getUnqualifiedJarName(jarURL) );
                                                }
                                                if ( persistenceUnitName == null && xmls.hasMoreElements() ) {
                                                    throw new PersistenceException( "No name provided and several persistence units found" );
                                                }
                                                else if ( persistenceUnitName == null || metadata.getName().equals( persistenceUnitName ) ) {
                                                    if (scanner == null) {
                                                        scanner = buildScanner( metadata.getProps(), integration );
                                                        jarURL = JarVisitorFactory.getJarURLFromURLEntry( url, "/META-INF/persistence.xml" );
                                                    }
                                                    //scan main JAR
                                                    ScanningContext mainJarScanCtx = new ScanningContext()
                                                            .scanner( scanner )
                                                            .url( jarURL )
                                                            .explicitMappingFiles( metadata.getMappingFiles() )
                                                            .searchOrm( true );
                                                    setDetectedArtifactsOnScanningContext( mainJarScanCtx, metadata.getProps(), integration,
                                                                                                        metadata.getExcludeUnlistedClasses() );
                                                    addMetadataFromScan( mainJarScanCtx, metadata );

                         

                                                    ScanningContext otherJarScanCtx = new ScanningContext()
                                                            .scanner( scanner )
                                                            .explicitMappingFiles( metadata.getMappingFiles() )
                                                            .searchOrm( true );
                                                    setDetectedArtifactsOnScanningContext( otherJarScanCtx, metadata.getProps(), integration,
                                                                                                        false );
                                                    for ( String jarFile : metadata.getJarFiles() ) {
                                                        otherJarScanCtx.url( JarVisitorFactory.getURLFromPath( jarFile ) );
                                                        addMetadataFromScan( otherJarScanCtx, metadata );
                                                    }
                                                    return configure( metadata, integration );
                                                }
                                            }
                                        }
                                    }
                                    return null;
                                }
                                catch (Exception e) {
                                    if ( e instanceof PersistenceException) {
                                        throw (PersistenceException) e;
                                    }
                                    else {
                                        throw new PersistenceException( getExceptionHeader() + "Unable to configure EntityManagerFactory", e );
                                    }
                                }
                            }

                        • 9. Re: Application Managed EntityManager JBoss AS 7.1.1
                          mbuamuh

                          The error happens in org.hibernate.ejb.packaging.PersistenceXmlLoader.depoly(...) For some reason the whole persistence.xml file is not read. Just the top of it is read. Any headway on this???

                           

                          public static List<PersistenceMetadata> deploy(URL url, Map overrides, EntityResolver resolver,

                                                                             PersistenceUnitTransactionType defaultTransactionType) throws Exception {

                                  Document doc = loadURL( url, resolver );

                                  Element top = doc.getDocumentElement();

                                  //version is mandatory

                                  final String version = top.getAttribute( "version" );

                           

                                  NodeList children = top.getChildNodes();

                                  ArrayList<PersistenceMetadata> units = new ArrayList<PersistenceMetadata>();

                                  for ( int i = 0; i < children.getLength() ; i++ ) {

                                      if ( children.item( i ).getNodeType() == Node.ELEMENT_NODE ) {

                                          Element element = (Element) children.item( i );

                                          String tag = element.getTagName();

                                          if ( tag.equals( "persistence-unit" ) ) {

                                              PersistenceMetadata metadata = parsePersistenceUnit( element );

                                              metadata.setVersion(version);

                                              //override properties of metadata if needed

                                              if ( overrides.containsKey( AvailableSettings.PROVIDER ) ) {

                                                  String provider = (String) overrides.get( AvailableSettings.PROVIDER );

                                                  metadata.setProvider( provider );

                                              }

                                              if ( overrides.containsKey( AvailableSettings.TRANSACTION_TYPE ) ) {

                                                  String transactionType = (String) overrides.get( AvailableSettings.TRANSACTION_TYPE );

                                                  metadata.setTransactionType( PersistenceXmlLoader.getTransactionType( transactionType ) );

                                              }

                                              if ( overrides.containsKey( AvailableSettings.JTA_DATASOURCE ) ) {

                                                  String dataSource = (String) overrides.get( AvailableSettings.JTA_DATASOURCE );

                                                  metadata.setJtaDatasource( dataSource );

                                              }

                                              if ( overrides.containsKey( AvailableSettings.NON_JTA_DATASOURCE ) ) {

                                                  String dataSource = (String) overrides.get( AvailableSettings.NON_JTA_DATASOURCE );

                                                  metadata.setNonJtaDatasource( dataSource );

                                              }

                                              /*

                                               * if explicit => use it

                                               * if JTA DS => JTA transaction

                                               * if non JTA DA => RESOURCE_LOCAL transaction

                                               * else default JavaSE => RESOURCE_LOCAL

                                               */

                                              PersistenceUnitTransactionType transactionType = metadata.getTransactionType();

                                              Boolean isJTA = null;

                                              if ( StringHelper.isNotEmpty( metadata.getJtaDatasource() ) ) {

                                                  isJTA = Boolean.TRUE;

                                              }

                                              else if ( StringHelper.isNotEmpty( metadata.getNonJtaDatasource() ) ) {

                                                  isJTA = Boolean.FALSE;

                                              }

                                              if (transactionType == null) {

                                                  if (isJTA == Boolean.TRUE) {

                                                      transactionType = PersistenceUnitTransactionType.JTA;

                                                  }

                                                  else if (isJTA == Boolean.FALSE) {

                                                      transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;

                                                  }

                                                  else {

                                                      transactionType = defaultTransactionType;

                                                  }

                                              }

                                              metadata.setTransactionType( transactionType );

                                              Properties properties = metadata.getProps();

                                              ConfigurationHelper.overrideProperties( properties, overrides );

                                              units.add( metadata );

                                          }

                                      }

                                  }

                                  return units;

                              }

                          • 10. Re: Application Managed EntityManager JBoss AS 7.1.1
                            smarlow

                            Could you try again with the EAP alpha built from AS7.  Download link is here.

                             

                            In 7.1.1, I don't think the persistence provider is loaded until its needed (an EE JPA container managed deployment is completed).  With the later AS7 version (link to EAP alpha above), we load the Hibernate persistence provider early during server startup (meaning the Persistence static api providers list should have Hibernate added to it). 

                            • 11. Re: Application Managed EntityManager JBoss AS 7.1.1
                              mbuamuh

                              Hi Scott,

                               

                              Unfortunately i can't run my app on EAP 6.1 Alpha due to some WeldListener null pointer exception. I have started a discussion about this on the community page https://community.jboss.org/message/807700#807700. But unfortunately till now i haven't received any response. Thats why i am still on JBoss AS 7.1.1. Does this mean that the application managed entity manager issue cannot be resolved in AS 7.1.1?

                              • 12. Re: Application Managed EntityManager JBoss AS 7.1.1
                                smarlow

                                You could try the latest nightly (AS8) build from here.