2016-01-19 1 views
1

Je voudrais utiliser deux bases de données dans le projet Spring. J'utilise spring-servlet.xml et applicationContext.xml pour appeler la base de données mySql. Comment puis-je faire cela?Utilisation de deux bases de données au printemps

Voici mon 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="[email protected]" /> 



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

S'il vous plaît aidez-moi à résoudre ce problème. Merci

+1

Vous devez déclarer autant de sources de données, de fabrique de sessions et de gestionnaire de transactions que vous avez de bases de données. –

Répondre

0

Comme bigdestroy correctement mentionné, vous devez utiliser plusieurs beans sessionFactory et transactionManager qui se réfèrent aux différentes sources de données.

Voir ce blogpost pour plus d'informations.