简体   繁体   English

一个项目中的多个数据源

[英]Multiple data sources in one project

I am working on a java project in which I have to store some data in a DB or another according to a value in a variable.我正在研究 java 项目,其中我必须根据变量中的值将一些数据存储在数据库或另一个数据库中。 For the management of databases I use myBatis.对于数据库的管理,我使用 myBatis。

The configuration to the databases:数据库的配置:

context.xml上下文.xml

<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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">

<context:component-scan base-package="com.nombreCompañia.ajax.service" />

<context:annotation-config />

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
        <value>java:comp/env/jdbc/DB1</value>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

   <bean id="dataSource_DB2" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
        <value>java:comp/env/jdbc/DB2</value>
    </property>
</bean>

<bean id="transactionManager_DB2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource_DB2" />
</bean>

<tx:annotation-driven />

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="sqlSessionFactory_DB2" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource_DB2" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.iberdrola.persistence.dao" />
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>   

I declare the resources in web.xml我在 web.xml 中声明资源

<resource-ref id="ResourceRef_5">
    <description>Base de datos 1</description>
    <res-ref-name>jdbc/DB1</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

<resource-ref id="ResourceRef_0">
    <description>Base de datos 2</description>
    <res-ref-name>jdbc/DB2</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

I have it this way because for database 1 I have not had any problem and it works correctly for me, so I decided to duplicate the configuration for the second DB, modifying its own configuration.我这样做是因为对于数据库 1 我没有任何问题并且它对我来说可以正常工作,所以我决定复制第二个数据库的配置,修改它自己的配置。

But how do I change at runtime to use one database or another?但是如何在运行时更改以使用一个或另一个数据库?

I have searched the internet a lot and cannot find a solution.我在互联网上搜索了很多,找不到解决方案。

I hope your help.我希望你的帮助。

Thank you谢谢

Spring's AbstractRoutingDataSource is what you are looking for. Spring 的AbstractRoutingDataSource是您正在寻找的。 It can be used to dynamically change the datasource at runtime.它可用于在运行时动态更改数据源。 There are lots of examples available on stack overflow and other places on the web. web 上有很多关于堆栈溢出和其他地方的示例。

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

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