I am working on a java project in which I have to store some data in a database or another according to a value in a variable. For the management of BBDD I use myBatis.
The configuration to the DBs:
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/pathApp" reloadable="true">
<Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver" maxIdle="1" maxTotal="20" maxWaitMillis="-1" name="jdbc/DB1" password="pass1" removeAbandonedOnBorrow="true" removeAbandonedTimeout="60" type="javax.sql.DataSource" url="ulrBD1" username="usernameDB1"/>
<Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver" maxIdle="1" maxTotal="20" maxWaitMillis="-1" name="jdbc/DB2" password="pass2" removeAbandonedOnBorrow="true" removeAbandonedTimeout="60" type="javax.sql.DataSource" url="urlDB2" username="usernameDB2"/>
</Context>
context-dfg.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
<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 problems and it works correctly, so I decided to duplicate the configuration for the second DB, modifying its own configuration.
But how do I change at runtime to use one database or another?
I've searched a lot on the internet and I can't find a solution.
I hope your help.
Thank you
Once I had to do what you mention and based it on this documentation/guide and it worked fine
https://www.baeldung.com/spring-data-jpa-multiple-databases
How do you use mybatis but I delegated the management of which DB to use to spring .