2

I have two databases for two different organizations with same database tables and objects. Now the application need to support both DB. Each user is attached to one organization and based on his login id application need to connect to particular DB and perform same operations.

So all Spring beans remain the same except target Database. How to do that most efficient way. I can think of creating multiple EntityManagerFactory in spring applicationContext file and in DAO I can select particular entitymanager based on user id/name (by passing it as an argument etc)

But what will be most efficient/correct way if we consider second level caching etc.

?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation=
    "http://www.springframework.org/schema/jdbc         http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd">
    <tx:annotation-driven />

    <bean id="empSvc" class="com.techcielo.sampleproject.service.EmployeeService">
        <property name="empDao" ref="empDao"></property>
    </bean>

    <bean id="empDao" class="com.techcielo.sampleproject.dao.EmployeeDAO">
        <property name="fac" ref="entityManagerFactory"></property>
    </bean>


    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
       <property name="persistenceXmlLocation" value="config/persistence.xml"></property>
       <property name="dataSource" ref="dataSource_2" />
       <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
       </property>
    </bean>

    <!-- Create one more entitymanager factory here with _2 and rename previous one with _1 -->

    <bean id="dataSource_1"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/northwind" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <bean id="dataSource_2"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/northwind_dup" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>
</beans>

Thanks in advance.

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.