繁体   English   中英

在Spring和Hibernate中使用多个数据源

[英]Using multiple DataSources in Spring and Hibernate

我有一个主数据源,在这里我具有读/写权限,另一个数据源,在这里我只是将数据拉出并导入到我的主数据源中。

目前,我的整个应用程序仅使用读/写数据库。 因此,我这样配置它:

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <context:annotation-config />

    <context:component-scan base-package="com.limitCalculator"
        annotation-config="true" />

    <tx:annotation-driven transaction-manager="transactionManager"
        proxy-target-class="true" />

    <bean id="mainGUI" class="com.limitCalculator.gui.scenarioSelection.MainWindow" />

    <!-- 1 Data Source -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
        <property name="url" value="jdbc:hsqldb:hsql://localhost/testDB" />
        <property name="username" value="sa" />
        <property name="password" value="" />
        <property name="initialSize" value="5" />
        <property name="maxActive" value="10" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="jpaData" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

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

    <bean id="entityManager"
        class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <!-- 2 Data Source -->
    <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
        <property name="url"
            value="jdbc:oracle:thin:@test.com.vi:1234:TEST" />
        <property name="username" value="test_db" />
        <property name="password" value="abcde" />
        <property name="maxActive" value="20" />
    </bean>
</beans>

我的DAO电话:

@Component
public class SettingsDaoImpl {

    @Autowired
    @PersistenceContext    
    public EntityManager em;

    public SettingsDaoImpl() {
        super();
    }

    @Transactional
    public Long save(Settings sr)
    {
        em.persist(sr);
        return sr.getId();
    }

如您所见,我在应用程序中添加了第二个数据库。 但是,我不知道如何在SettingsDaoImpl中正确调用它?

任何建议如何在我当前的体系结构中实现这一点。

您需要为每个数据源创建不同的实体管理器,并且需要在代码中定义@PersistenceContext的unitName属性以注入特定的实体管理器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM