我有两个不同组织的数据库,它们具有相同的数据库表和对象 . 现在应用程序需要同时支持DB . 每个用户都连接到一个组织,并根据他的登录ID应用程序需要连接到特定的DB并执行相同的操作 .

因此除了目标数据库之外,所有Spring bean都保持不变 . 如何以最有效的方式做到这一点 . 我可以考虑在spring applicationContext文件中创建多个EntityManagerFactory,在DAO中我可以根据用户id / name选择特定的entitymanager(通过将其作为参数传递等)

但是,如果我们考虑二级缓存等,那么最有效/正确的方法是什么 .

?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>

提前致谢 .