7.2. Migrating Spring to Red Hat JBoss Enterprise Application Platform from a Servlet Container
- Ensures that applications are compatible with the requirements of Java EE 6 standard
- Enables integration with the Red Hat JBoss middleware services provided by the container
- Avoids the inclusion of libraries that are provided by the application server
- Session factories
- Datasources
- Entity managers
7.2.1. Avoiding the Inclusion of Server-provided Dependencies
- The Java EE 6 APIs (like JPA, JSTL, and JMS)
- Hibernate (including Hibernate as a JPA provider)
- JSF
- Spring Framework
- Snowdrop
- Facelets
- RichFaces
7.2.2. Migrating Datasource Definitions
Example 7.1. Example commons-dbcp DataSource definition in a servlet container
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.postgresql.Driver" /> <property name="url" value="jdbc:postgresql://exampleHost/exampleDatabase" /> <property name="username" value="user" /> <property name="password" value="password" /> </bean>
Example 7.2. Using a JBoss Enterprise Application Platform managed datasource in Spring
<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/ExampleDS" expected-type="javax.sql.DataSource"/>
7.2.3. Migrating Hibernate SessionFactories to JTA
Example 7.3. SessionFactory and transaction manager definitions in a servlet environment
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingLocations" value="classpath:**/*.hbm.xml"/> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect hibernate.show_sql=true </value> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
Example 7.4. JTA-based SessionFactory and transaction manager
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingLocations" value="classpath:**/*.hbm.xml"/> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect hibernate.show_sql=true hibernate.current_session_context_class=jta hibernate.transaction.jta.platform=org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory </value> </property> </bean> <tx:jta-transaction-manager id="transactionManager"/>
Note
7.2.4. Migrating JPA-based Applications
META-INF/persistence.xml file is automatically deployed by the container, but the container cannot declare a RESOURCE_LOCAL transaction type, and must include a JTA datasource reference. Also, the container may not specify a transaction type (which is equivalent to setting a JTA transaction type, but is ignored when the persistence unit is initialized by Spring and a resource-local model is used instead).
Example 7.5. A sample persistence unit definition for a servlet-container based application
<persistence-unit name="examplePU" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/> </properties> </persistence-unit>
Example 7.6. JPA EntityManagerFactory and transaction setup in a servlet-container based application
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource"> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:database="${jpa.database}" p:showSql="${jpa.showSql}"/> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory"/>
META-INF/persistence.xml file.
- rename the persistence unit definition file
- leave persistence-unit deployment to JBoss Enterprise Application Platform and use JNDI lookup for retrieving entity managers and entity manager factories.
Example 7.7. LocalContainerEntityManagerFactoryBean with alternate persistence.xml location
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceXmlLocation" value="classpath*:META-INF/jpa-persistence.xml"/> <!-- other properties (ommitted) --> </bean>
persistence.xml file definition to a JTA-based model and using JNDI lookup for retrieving the entity manager. For this, convert the persistence unit definition as in the following example (note that it is not necessary to provide values for both jboss.entity.manager.jndi.name and jboss.entity.manager.factory.jndi.name, but one must be specified).
Example 7.8. Changing the persistence.xml definition to be Java EE 6 compatible
<persistence-unit name="examplePU"> <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source> <properties> <property name="hibernate.show_sql" value="true"/> <property name="jboss.entity.manager.jndi.name" value="java:jboss/example/EntityManager"/> <property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/example/EntityManagerFactory"/> </properties> </persistence-unit>
Example 7.9. EntityManager retrieved by JNDI lookup and JTA transaction manager (works with @Autowired)
<jee:jndi-lookup id="entityManager" jndi-name="java:jboss/example/EntityManager" expected-type="javax.persistence.EntityManager"/> <tx:jta-transaction-manager/>
Example 7.10. EntityManagerFactory retrieved by JNDI lookup and JTA transaction manager (works with @PersistenceContext)
<jee:jndi-lookup id="entityManagerFactory" jndi-name="java:jboss/example/EntityManagerFactory" expected-type="javax.persistence.EntityManagerFactory"/> <tx:jta-transaction-manager/>