0
votes

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.

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.

Below are the configuration details

persistence.xml

<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<class>com.test.User</class>
<properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>

spring-application-context.xml

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<jee:jndi-lookup id="dataSource" jndi-name="java:/datasources/test" />

<bean id="entityManagerFactory"
 class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="generateDdl" value="false" />
    <property name="database" value="MYSQL" />
            <property name="showSql" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
    </bean>
</property>
<property name="jpaPropertyMap">
    <map>
    <entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"></entry>
    <entry key="hibernate.current_session_context_class" value="jta" />
    <entry key="hibernate.connection.release_mode" value="auto" />
   </map>
</property>
<property name="persistenceUnitPostProcessors">
       <list>
        <bean class="com.transaction.processor.JtaPersistenceUnitPostProcessor">
                <property name="jtaMode" value="true"/> 
            <property name="jtaDataSource" ref="dataSource"/>
        </bean>
       </list>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:/TransactionManager"></property>
<property name="autodetectUserTransaction" value="false"></property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

The class JtaPersistenceUnitPostProcessor is responsible for setting the transaction-type as JTA and the datasource to jta-datasource.

Could anyone please provide any help on this.

Thanks in advance.

2

2 Answers

0
votes
 <bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManagerName" value="java:jboss/TransactionManager" />
    <property name="userTransactionName" value="java:comp/UserTransaction" />
  </bean>

 <tx:annotation-driven transaction-manager="transactionManager" />


 <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

you didn't specify any error message . you can add these lines in your configuration file .

0
votes

I see you use JTA transaction manager and use that only if you use distributed Transaction and use JNDI. JTA tran. manager listens TX happening through connection acquired from JNDI datasource. If you have datasource created in your code and is not a part of Web container but is limited inside app. container in your web server, JTA wont work.

If you want to implement Tx manager with in a single app. context go for JPA transaction manager which is very reliable.