多个数据源配置问题

时间:2018-04-25 12:51:16

标签: spring oracle hibernate jpa

在我的项目中,我使用两个oracle数据源作为其中一个数据源Im使用def scriptPath = currentBuild.rawBuild.parent.definition.scriptPath 和另一个LocalSessionFactoryBean 我正在相应地映射数据源,但是当我试图获取LocalContainerEntityManagerFactoryBean的sessionFactory时,数据源详细信息是另一个的详细信息。多诺我做错了。

LocalSessionFactoryBean

在我导入的另一个xml文件中

<tx:annotation-driven transaction-manager="xxxTransactionManager" />

    <bean id="xxxEntityManagerFactoryBean"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />     
        <property name="persistenceXmlLocation" value="classpath:persistence.xml" />

        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>

            </props>
        </property>
    </bean>
    <bean class="org.modelmapper.ModelMapper" id="modelMapper"/>
    <bean id="xxxEntityManager"
        class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="xxxEntityManagerFactoryBean" />
    </bean>

    <bean id="xxxTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="xxxEntityManagerFactoryBean" />
        <property name="nestedTransactionAllowed" value="true" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>
    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

在app context.xml中我有像

这样的数据源
<bean id="sessionFactoryNew"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource1" />
      <property name="packagesToScan" value="com.citi.aml.npa.domain.api" />
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
         </props>
      </property>
   </bean>
   <bean id="xxxTransactionManagerNew"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactoryNew" />
      <qualifier value="xxxtransaction"/>
   </bean>

在Dao impl calss我有

<beans profile="local">
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="${db.oracle.driverClassName}" />
            <property name="username" value="${db.oracle.username}" />
            <property name="password" value="${db.oracle.password}" />
            <property name="url" value="${db.oracle.url}" />

        </bean>
        <context:property-placeholder location="classpath*:/config/local/yyy.properties" ignore-unresolvable="true"/>
    </beans>
<beans profile="local">
        <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="${db.oracle.driverClassName}" />
            <property name="username" value="${db.oracle.username}" />
            <property name="password" value="${db.oracle.password}" />
            <property name="url" value="${db.oracle.url}" />

        </bean>
        <context:property-placeholder location="classpath*:/config/local/xxx_db.properties" ignore-unresolvable="true"/>
    </beans>

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

你的bean配置对我来说有点不对劲。您有两个具有相同名称的不同配置文件。如果这两个数据源需要同时运行,那么将数据库属性合并到一个文件中会更简单,并将两个数据源配置放在同一个配置文件元素中。

因此,对于统一的database.properties,请执行以下操作:

db1.oracle.url=jdbc:oracle:thin:@server1...
db1.oracle.username=foo
...
db2.oracle.url=jdbc:oracle:thin:@server2...
db2.oracle.username=bar 

对于bean配置,请使用类似:

的内容
<beans profile="local">
        <context:property-placeholder location="classpath*:/config/local/database.properties" ignore-unresolvable="true"/>
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="${db1.oracle.driverClassName}" />
            <property name="username" value="${db1.oracle.username}" />
            <property name="password" value="${db1.oracle.password}" />
            <property name="url" value="${db1.oracle.url}" />

        </bean>
        <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="${db2.oracle.driverClassName}" />
            <property name="username" value="${db2.oracle.username}" />
            <property name="password" value="${db2.oracle.password}" />
            <property name="url" value="${db2.oracle.url}" />
        </bean>
    </beans>