1

I am facing an issue with the injection of EntityManager to DAO without using @PersistenceContext.

I am working on an application that has two EntityManagerFactories one which use a jndi datasource and another which uses a jdbc datasource. I have to inject the EntityManager to DAO without using @PersistenceContext, since I want to use the same DAO for both datasources. Both the persistence units are in the same persistence.xml

The approach that I followed is:

persistenceContext-jdbc.xml

<tx:annotation-driven transaction-manager="demoTransactionManager" />
<bean id="demoEntityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="beanName" value="demoEntityManager" />
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="jdbcPersistence" />
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="false" />
            <property name="databasePlatform" value="org.apache.openjpa.jdbc.sql.DB2Dictionary" />
        </bean>
    </property>
    <property name="loadTimeWeaver">
        <bean
            class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
</bean>

<bean id="demoDAO" class="com.test.dao.DemoDAO"
    <property name="demoEntityManager">
    <bean class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="demoEntityManagerFactory"/>  
    </bean>
    </property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
    ....database properties....
</bean>

<bean id="demoTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="demoEntityManagerFactory" />
</bean>

persistenceContext-jndi.xml

<bean id="demoEntityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="beanName" value="demoEntityManager" />
    <property name="persistenceUnitName" value="jndiPersistence" />
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="false" />
            <property name="databasePlatform" value="org.apache.openjpa.jdbc.sql.DB2Dictionary" />
        </bean>
    </property>
</bean>

<bean id="demoDAO" class="com.test.dao.DemoDAO">
    <property name="demoEntityManager">
    <bean id="demoEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="demoEntityManagerFactory"/>  
    </bean>
    </property>
</bean> 

Though I am able to inject the EntityManager for jdbc flow. I am facing issues with the same for jndi flow.

The application polls messages from MQ using spring integration (@ServiceActivator).

The following is the exception that i am getting:

Setup of JMS message listener invoker failed for destination 'queue://<QueueName>' - trying to recover. Cause: Could not open JPA EntityManager for transaction; nested exception is <openjpa-2.2.2-r422266:1468616 nonfatal user error> org.apache.openjpa.persistence.InvalidStateException: You cannot access the EntityTransaction when using managed transactions.
Indrajeet
  • 5,490
  • 2
  • 28
  • 43
n611
  • 11
  • 5
  • Let us know how does that work when you use `` instead of `JpaTransactionManager` – Artem Bilan Apr 10 '15 at 14:07
  • I have used JpaTransactionManager only in case of jdbc datasource. – n611 Apr 10 '15 at 14:44
  • For jndi datasource, I have used – n611 Apr 10 '15 at 14:44
  • And? And `DataSource` are feasible with ``. Since you try to use several transactional resources there (JPA, JDBC, JMS) you really need some global TX controller. It is XA for such cases and JTA does the stuff on the matter! – Artem Bilan Apr 10 '15 at 15:03

0 Answers0