【问题标题】:Using two databases in Spring在 Spring 中使用两个数据库
【发布时间】:2016-01-19 05:06:03
【问题描述】:

我想在 Spring 项目中使用两个数据库。 我正在使用 spring-servlet.xml 和 applicationContext.xml 来调用 mySql 数据库。 我该怎么做?

这是我的 spring-servlet.xml。

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

<context:component-scan base-package="com.startinpoint.proj.PMS" />

<tx:annotation-driven transaction-manager="transactionManager" />
<!-- <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> -->

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactory" />
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://00.00.00.0.00/OMS_DEV"
    p:username="sipadmin" p:password="P@ger123" />



<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />

    <property name="annotatedClasses">
       <list>
            <value>com.startinpoint.proj.PMS.ProjectModule.Project</value>
            <value>com.startinpoint.proj.PMS.PhaseModule.Phase</value>
            <value>com.startinpoint.proj.PMS.TaskModule.Task</value>
       </list>
    </property>

    <!-- 
    <property name="configLocation">
        <value>hibernate.cfg.xml</value>
    </property>
    -->
    <property name="hibernateProperties">
       <props>
           <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
           <prop key="hibernate.show_sql">false</prop>
       </props>
    </property>
</bean>

 <bean id="projectDAO" class="com.startinpoint.proj.PMS.ProjectModule.ProjectDAOImpl">
    <property name="sessionFactory" ref="sessionFactory" />
 </bean>

 <!-- Phase -->
    <bean id="phaseBean" class="com.startinpoint.proj.PMS.PhaseModule.PhaseBean">
    <property name="phaseManager" ref="phaseManager" />
    </bean>
  <bean id="phaseManager" class="com.startinpoint.proj.PMS.PhaseModule.PhaseManager">
    <property name="phaseDAO" ref="phaseDAO" />
 </bean>

 <bean id="phaseDAO" class="com.startinpoint.proj.PMS.PhaseModule.PhaseDAOImpl">
    <property name="sessionFactory" ref="sessionFactory" />
 </bean>

 <!-- Task -->
   <bean id="taskBean" class="com.startinpoint.proj.PMS.TaskModule.TaskBean">
    <property name="taskManager" ref="taskManager" />
    </bean>
  <bean id="taskManager" class="com.startinpoint.proj.PMS.TaskModule.TaskManager">
    <property name="taskDAO" ref="taskDAO" />
 </bean>

 <bean id="taskDAO" class="com.startinpoint.proj.PMS.TaskModule.TaskDAOImpl">
    <property name="sessionFactory" ref="sessionFactory" />
 </bean> 

请帮我解决这个问题。谢谢

【问题讨论】:

  • 您必须声明与您拥有的数据库一样多的数据源、会话工厂和事务管理器。

标签: spring multiple-databases


【解决方案1】:

正如 bigdestroy 正确提到的,您应该使用多个引用不同数据源的 sessionFactorytransactionManager bean。

更多信息请参见blogpost

【讨论】:

    猜你喜欢
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多