title: EJB 3.0 -> Spring -> JPA (JTA as transaction manager)
body: <p>I am currently working on a project that includes EJB 3.0 (stateless SB), JPA (Hibernate as the provider), JTA as transaction manager. The app server is JBoss AS 7. Spring is used for integrating EJB and JPA.</p>
  
  <p>All seems to be working fine, except if there is any exception that occurs in the EJB, then the persistence unit is closed by Spring. On the subsequent request, the persistence unit is again created, which becomes time consuming and also should not happen in the ideal situation.</p>
  
  <p>Below are the configuration details</p>
  
  <p>persistence.xml</p>
  
  <pre><code>&lt;persistence-unit name="test" transaction-type="RESOURCE_LOCAL"&gt;
  &lt;class&gt;com.test.User&lt;/class&gt;
  &lt;properties&gt;
      &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /&gt;
  &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  
  spring-application-context.xml
  
  &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;
  
  &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/&gt;
  
  &lt;jee:jndi-lookup id="dataSource" jndi-name="java:/datasources/test" /&gt;
  
  &lt;bean id="entityManagerFactory"
   class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
  &lt;property name="dataSource" ref="dataSource" /&gt;
  &lt;property name="jpaVendorAdapter"&gt;
      &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
      &lt;property name="generateDdl" value="false" /&gt;
      &lt;property name="database" value="MYSQL" /&gt;
              &lt;property name="showSql" value="true" /&gt;
              &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/&gt;
      &lt;/bean&gt;
  &lt;/property&gt;
  &lt;property name="jpaPropertyMap"&gt;
      &lt;map&gt;
      &lt;entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"&gt;&lt;/entry&gt;
      &lt;entry key="hibernate.current_session_context_class" value="jta" /&gt;
      &lt;entry key="hibernate.connection.release_mode" value="auto" /&gt;
     &lt;/map&gt;
  &lt;/property&gt;
  &lt;property name="persistenceUnitPostProcessors"&gt;
         &lt;list&gt;
          &lt;bean class="com.transaction.processor.JtaPersistenceUnitPostProcessor"&gt;
                  &lt;property name="jtaMode" value="true"/&gt; 
              &lt;property name="jtaDataSource" ref="dataSource"/&gt;
          &lt;/bean&gt;
         &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="transactionManager"
      class="org.springframework.transaction.jta.JtaTransactionManager"&gt;
  &lt;property name="transactionManagerName" value="java:/TransactionManager"&gt;&lt;/property&gt;
  &lt;property name="autodetectUserTransaction" value="false"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  </code></pre>
  
  <p>The class JtaPersistenceUnitPostProcessor is responsible for setting the transaction-type as JTA and the datasource to jta-datasource.</p>
  
  <p>Could anyone please provide any help on this.</p>
  
  <p>Thanks in advance.</p>