Results 1 to 3 of 3

Thread: Multiple Entity Managers in the same application not working

  1. #1
    Join Date
    Feb 2009
    Posts
    12

    Default Multiple Entity Managers in the same application not working

    Hi All,

    I am using two entity managers in my applicationContext.xml. I have tow tables having same name(tbl_states). I am able to query the database from the default datasource but not from the other one. Here is my applicationContext.xml sample code:

    <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" />
    <property name="url"
    value="jdbc:db2://Hostname1ort/dbname:INFORMIXSERVER=connection_name;DELIMIDENT=y ;" />
    <property name="username" value="username" />
    <property name="password" value="password" />
    <property name="minIdle" value="2" />
    </bean>

    <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" />
    <property name="url"
    value="jdbc:db2://Hostname2ort/dbname2:INFORMIXSERVER=connection_name;DELIMIDENT= y;" />
    <property name="username" value="username" />
    <property name="password" value="password" />
    <property name="minIdle" value="2" />
    </bean>
    <bean id="persistenceUnitManager"
    class="org.springframework.orm.jpa.persistenceunit .DefaultPersistenceUnitManager">
    <property name="persistenceXmlLocations">
    <list>
    <value>classpath*:META-INF/persistence.xml</value>
    <value>classpath*:META-INF/persistence2.xml</value>

    </list>
    </property>
    <property name="dataSources">
    <map>
    <entry key="localDataSource" value-ref="dataSource1" />
    <entry key="dataSource1" value-ref="dataSource2" />
    </map>
    </property>
    <property name="defaultDataSource" ref="dataSource2" />
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionM anager">
    <property name="entityManagerFactory" ref="entityManagerFactory2" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
    <tx:annotation-driven transaction-manager="transactionManager2" />

    <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource1" />
    <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.Hibernat eJpaVendorAdapter">
    <property name="database" value="INFORMIX" />
    <property name="showSql" value="true" />
    </bean>
    </property>
    <property name="persistenceUnitManager" ref="persistenceUnitManager" />
    <property name="persistenceUnitName" value="PU1" />
    </bean>



    <bean id="entityManagerFactory2"
    class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource2" />
    <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.Hibernat eJpaVendorAdapter">
    <property name="database" value="INFORMIX" />
    <property name="showSql" value="true" />
    </bean>
    </property>
    <property name="persistenceUnitManager" ref="persistenceUnitManager" />
    <property name="persistenceUnitName" value="PU2" />
    </bean>

    And I have defined two entities com.companyname.entity1.TblStates and com.companyname.entity2.TblStates which corresponds to the tables in database1 and database2. If I query the database1 its working fine but if I define the entitymanger as PU2 in anothere DAO layer then I am getting nothing. Any suggestions?
    Please help!

  2. #2
    Join Date
    Feb 2009
    Posts
    12

    Default

    So it means no body have faced such problem? Please give me your thoughts on how to approach this. I really need to get it done

  3. #3
    Join Date
    Dec 2012
    Posts
    2

    Default

    I am facing a very similar issue.

    I have two datasources, two EntityManagerFactory's, two TransactionManagerFactory's. The application commits to one of the database but not the other. There are no errors thrown.

    Has anyone faced this problem?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •