Help

Here are a few quick tips when migrating Hibernate 3-based applications to JBoss AS7, covering the options available for JBoss AS 7.0.0.Final.

In previous versions of JBoss AS, a version of Hibernate was always present on the classpath. As a result, applications that used Hibernate directly have traditionally avoided to include it in the deployment in order to avoid classloading conflicts, or have taken additional steps to enforce classloader isolation.

The visibility rules have changed in AS7 due to the new modular classloading model, and so Hibernate libraries are made visible by default only to JPA deployments. The changes may affect existing Hibernate applications, so this is a short breakdown on the available options when running on JBoss AS7. Wherever this is subject to change in future versions, we also provide a succint description of the forthcoming enhancements.

Native Hibernate applications

As of the 7.0.0.Final version of JBoss AS, native (i.e. non-JPA) Hibernate applications can opt between:

  • packaging a Hibernate version of their choice inside the application;
  • adding the 'org.hibernate' module as a dependency to the application (adds Hibernate 4), e.g. by adding to the deployment a META-INF/MANIFEST.MF file that contains the following line:
    Dependencies: org.hibernate
  • creating a custom Hibernate 3 module, and adding it as a dependency to the application,e.g.
    Dependencies: my-custom-hibernate-module
    - noting that future versions of JBoss AS7 may already provide a skeleton Hibernate 3 module.

Application-created persistence units

JPA applications that create an EntityManagerFactory on their own, either using the PersistenceProvider SPI directly or through an intermediary mechanism such as Spring's LocalContainerEntityManagerFactoryBean have the same options as native Hibernate applications, noting that the 'org.hibernate' module may be added by default as a dependency as described here - so you may need to exclude it explicitly if you want to provide a version of your own. Therefore, under typical circumstances such applications can:

  • use the default setup (and use Hibernate 4)
  • package a Hibernate version of their choice and exclude the default 'org.hibernate' module by adding a WEB-INF/jboss-deployment-structure.xml or META-INF/jboss-deployment-structure.xml file (depending whether the deployment is a WAR or not) as follows:
    <jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0">
      <deployment>
        <exclusions>
           <module name="org.hibernate"/>
        </exclusions>
      <deployment>
    </jboss-deployment-structure>
  • create a custom module and using it instead of the default 'org.hibernate';

Container-deployed persistence units

Generally speaking, standard Java EE-applications may ignore the provider implementation and rely on the standard features provided by the container - JBoss AS7 supporting standard JPA 1.0 and 2.0.

However, in future versions of JBoss AS7 it will be possible to use alternative persistence provider implementations - for more details please follow AS7-566.

1 comment:
 
30. Jul 2011, 20:20 CET | Link
Kawu

Currently, excluding Hibernate 4 isn't working, at least when porting Seam 2 apps. I used

<jboss-deployment-structure> 
  <deployment>
    <exclusions>
      ...
      <module name="org.hibernate" slot="main" />
    </exclusions>
    ...
  </deployment>   
</jboss-deployment-structure>

but then a CCE occurrs when deploying:

20:07:23,921 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC00001: Failed to start service jboss.persistenceunit."bbstats.war#MyPersistenceUnit": org.jboss.msc.service.StartException in service jboss.persistenceunit."bbstats.war#MyPersistenceUnit": Failed to start service
	at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1786)
	at org.jboss.msc.service.ServiceControllerImpl$ClearTCCLTask.run(ServiceControllerImpl.java:2291)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [:1.7.0]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [:1.7.0]
	at java.lang.Thread.run(Unknown Source) [:1.7.0]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: MyPersistenceUnit] Unable to build EntityManagerFactory
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:903)
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:879)
	at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
	at org.jboss.as.jpa.service.PersistenceUnitService.createContainerEntityManagerFactory(PersistenceUnitService.java:170)
	at org.jboss.as.jpa.service.PersistenceUnitService.start(PersistenceUnitService.java:80)
	at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1765)
	... 4 more
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.service.jdbc.connections.spi.ConnectionProvider]
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:170)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:137)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:118)
	at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:208)
	at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:85)
	at org.hibernate.service.internal.BasicServiceRegistryImpl.configureService(BasicServiceRegistryImpl.java:80)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:143)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:118)
	at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:68)
	at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2251)
	at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2247)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1718)
	at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:77)
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:894)
	... 9 more
Caused by: org.hibernate.HibernateException: Could not instantiate connection provider [org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider]
	at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:190)
	at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:112)
	at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:54)
	at org.hibernate.service.internal.BasicServiceRegistryImpl.initiateService(BasicServiceRegistryImpl.java:68)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:160)
	... 22 more
Caused by: java.lang.ClassCastException: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider cannot be cast to org.hibernate.service.jdbc.connections.spi.ConnectionProvider
	at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:187)
	... 26 more

20:07:24,153 INFO  [org.jboss.as.server.controller] (DeploymentScanner-threads - 2) Deployment of "bbstats.war" was rolled back with failure message {"Failed services" => {"jboss.persistenceunit.\"bbstats.war#MyPersistenceUnit\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"bbstats.war#MyPersistenceUnit\": Failed to start service"}}
20:07:24,153 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 2) Service status report
  Services which failed to start:
      service jboss.persistenceunit."bbstats.war#MyPersistenceUnit": org.jboss.msc.service.StartException in service jboss.persistenceunit."bbstats.war#MyPersistenceUnit": Failed to start service

When removing the line

     <module name="org.hibernate" slot="main" />

everything deploys fine, but then me and others are stuck on another runtime exception, see TransactionManager lookup fails on AS7. No webapp on JBoss AS 7 yet...

Most people using Seam 2 porting to JBoss AS 7 will likely use Hibernate, so I consider my comment somewhat applicable. Feel free to shorten it as appropriate.

ReplyQuote
Post Comment