0

I am working on a batch application developed using Spring. This application have to read on a database and then have to write on another database.

So I know that I can create 2 different DAO with 2 different connections but I am trying to do it using Spring profiles.

So I have the following situation, I have a configuration file named databaseConfiguration.xml that contains the information for the DB connection (I import this file into the main configuration file applicationContext.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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">


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

    <!-- DB CONNECTIONS: -->
    <bean id="tjtJTransactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        scope="singleton">
        <property name="dataSource" ref="dataSourcePUC" />
    </bean>

    <!-- PROFILO DI PRODUZIONE -->
    <beans profile="PROD">

        <bean id="dataSourcePUC" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
            <property name="url" value="jdbc:jtds:sqlserver://XXX.myCompany.YYYY.it:1433/DB_1" />
            <property name="username" value="username" />
            <property name="password" value="pswd" />
        </bean>

    </beans>

    <!-- PROFILO DI COLLAUDO -->
    <beans profile="COLL">

        <bean id="dataSourcePUC" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
            <property name="url" value="jdbc:jtds:sqlserver://XXXX.MYCOMPANY.YYYY.it:1433/DB_2" />
            <property name="username" value="username" />
            <property name="password" value="pswd" />

        </bean>
    
    </beans>
   
</beans>

As you can see in the previous snippet I am defining 2 different profiles that defines 2 different version of the datasource bean with id="dataSourcePUC" that create the connection to my 2 different database.

So, into my main() method I do something like this:

GenericXmlApplicationContext context = new GenericXmlApplicationContext();
ConfigurableEnvironment conf = (ConfigurableEnvironment) context.getEnvironment();
conf.setActiveProfiles("PROD"); 
context.load("applicationContext.xml");
context.refresh();

pucManager = (PucManager) context.getBean("pucManager");  // my DAO

// QUERY PERFORMED ON THE DB_1:
List<TassoRendimentoInterno> tassoRendimentoInternoList = pucManager.getTassoRendimentoInterno();
    
// THIS SECOND QUERY HAVE TO BE PERFORMED ON THE DB_2
List<TassoInternoRendimentoFondo> tassoInternoRendimentoFondoList = pucManager.getTassoRendimentoInternoFondo();

In this code snippet first I set the active profile on PROD so it is used the DB_1, in this way:

ConfigurableEnvironment conf = (ConfigurableEnvironment) context.getEnvironment();
conf.setActiveProfiles("PROD");

It work fine and the query are performed on this DB.

The problem is that I want to perform the second query on the DB_2, so before this line:

List<TassoInternoRendimentoFondo> tassoInternoRendimentoFondoList = pucManager.getTassoRendimentoInternoFondo();

I have to set the other profile COLL.

To do this operation I also have to stop and refresh the context?

How can exactly do it? What is the best solution?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

0 Answers0