Page 1 of 1

initial multiple database connection not working

Posted: Mon Jan 27, 2014 1:15 pm
by tejnk
Hi,
I tried to connect to multiple databases by making appropriate entries in the xml files that would finally enable me to do something of the sort ...

Code: Select all

@PersistenceContext(unitName="blPU")
  protected EntityManager em;

@PersistenceContext(unitName="blPU2")
  protected EntityManager em2;


where blPU2 has been defined exactly in the same way as blPU in all the configuration files except that it points to a different database than that pointed to by blPU.
We are using Mysql and both blPU and blPU2 have the same schema.

However, save using em2 does not work - any pointers to how I should go about debugging and making multiple databases working, would be greatly appreciated.

thanks

Re: initial multiple database connection not working

Posted: Tue Jan 28, 2014 1:09 am
by phillipuniverse
Can you post the configuration that you added along with the exception that you got?

Re: initial multiple database connection not working

Posted: Wed Jan 29, 2014 9:24 am
by tejnk
Pls find below the configuration files...

persistence.xml

Code: Select all

<persistence-unit name="blPU" transaction-type="RESOURCE_LOCAL">
        <non-jta-data-source>jdbc/web</non-jta-data-source>
        <exclude-unlisted-classes/>
    </persistence-unit>
   
   <persistence-unit name="blPU2" transaction-type="RESOURCE_LOCAL">
       <non-jta-data-source>jdbc/web2</non-jta-data-source>
        <exclude-unlisted-classes/>
    </persistence-unit>


build.xml // used the same dialect for both blPU and blPU2

Code: Select all

<jpaconfiguration persistenceUnit="blPU" dialect="${ant.hibernate.sql.ddl.dialect}" />

<jpaconfiguration persistenceUnit="blPU2" dialect="${ant.hibernate.sql.ddl.dialect}" />   


applicationContext.xml

Code: Select all

<bean id="blMergedDataSources" class="org.springframework.beans.factory.config.MapFactoryBean">
        <property name="sourceMap">
            <map>
                <entry key="jdbc/web" value-ref="webDS"/>
                <entry key="jdbc/webSecure" value-ref="webSecureDS"/>
                <entry key="jdbc/cmsStorage" value-ref="webStorageDS"/>
                <entry key="jdbc/web2" value-ref="webDS2"/>
            </map>
        </property>
</bean>


jetty-env.xml

Code: Select all

<New id="webDS" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg>jdbc/web</Arg>
    <Arg>
        <New class="org.apache.commons.dbcp.BasicDataSource">
            <Set name="driverClassName">com.mysql.jdbc.Driver</Set>
            <Set name="url">jdbc:mysql://localhost:3306/database1</Set>
            <Set name="username">root</Set>
            <Set name="password">root</Set>
        </New>
    </Arg>
</New>


Code: Select all

<New id="webDS2" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg>jdbc/web2</Arg>
    <Arg>
        <New class="org.apache.commons.dbcp.BasicDataSource">
            <Set name="driverClassName">com.mysql.jdbc.Driver</Set>
            <Set name="url">jdbc:mysql://localhost:3306/database2</Set>
            <Set name="username">root</Set>
            <Set name="password">root</Set>
        </New>
    </Arg>
</New>


// both the databases - database1 and database2 have been created and both have the same schema

applicationContext-datasource.xml

Code: Select all

   <jee:jndi-lookup id="webDS" jndi-name="jdbc/web"/>
   
  <jee:jndi-lookup id="webDS2" jndi-name="jdbc/web2"/>


In CatalogDaoImpl, did not get any exception - however, it would connect only to 1 database
In ProductDaoImpl, got the exception ..while building with ant
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blProductDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'blPU2' is defined


thanks

Re: initial multiple database connection not working

Posted: Wed Jan 29, 2014 11:03 am
by phillipuniverse
You need an EntityManagerFactoryBean definition in an application context:

Code: Select all

    <bean id="blPU2EntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="blCacheManager">
        <property name="jpaVendorAdapter" ref="blJpaVendorAdapter"/>
        <property name="persistenceUnitManager" ref="blPersistenceUnitManager"/>
        <property name="persistenceUnitName" value="blPU2"/>
    </bean>