title: How can I automatically switch to a Spring profile to another one during the execution of an application? (to change the DB on which a DAO work)
body: <p>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.</p>
  <p>So I know that I can create 2 different DAO with 2 different connections but I am trying to do it using Spring profiles.</p>
  <p>So I have the following situation, I have a configuration file named <code>databaseConfiguration.xml</code> that contains the information for the DB connection (I import this file into the main configuration file <strong>applicationContext.xml</strong>):</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
      xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:jdbc=&quot;http://www.springframework.org/schema/jdbc&quot;
      xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
      xsi:schemaLocation=&quot;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&quot;&gt;
  
  
      &lt;tx:annotation-driven transaction-manager=&quot;tjtJTransactionManager&quot; /&gt;
  
      &lt;!-- DB CONNECTIONS: --&gt;
      &lt;bean id=&quot;tjtJTransactionManager&quot;
          class=&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;
          scope=&quot;singleton&quot;&gt;
          &lt;property name=&quot;dataSource&quot; ref=&quot;dataSourcePUC&quot; /&gt;
      &lt;/bean&gt;
  
      &lt;!-- PROFILO DI PRODUZIONE --&gt;
      &lt;beans profile=&quot;PROD&quot;&gt;
  
          &lt;bean id=&quot;dataSourcePUC&quot; class=&quot;org.apache.commons.dbcp.BasicDataSource&quot;
              destroy-method=&quot;close&quot;&gt;
              &lt;property name=&quot;driverClassName&quot; value=&quot;net.sourceforge.jtds.jdbc.Driver&quot; /&gt;
              &lt;property name=&quot;url&quot; value=&quot;jdbc:jtds:sqlserver://XXX.myCompany.YYYY.it:1433/DB_1&quot; /&gt;
              &lt;property name=&quot;username&quot; value=&quot;username&quot; /&gt;
              &lt;property name=&quot;password&quot; value=&quot;pswd&quot; /&gt;
          &lt;/bean&gt;
  
      &lt;/beans&gt;
  
      &lt;!-- PROFILO DI COLLAUDO --&gt;
      &lt;beans profile=&quot;COLL&quot;&gt;
  
          &lt;bean id=&quot;dataSourcePUC&quot; class=&quot;org.apache.commons.dbcp.BasicDataSource&quot;
              destroy-method=&quot;close&quot;&gt;
              &lt;property name=&quot;driverClassName&quot; value=&quot;net.sourceforge.jtds.jdbc.Driver&quot; /&gt;
              &lt;property name=&quot;url&quot; value=&quot;jdbc:jtds:sqlserver://XXXX.MYCOMPANY.YYYY.it:1433/DB_2&quot; /&gt;
              &lt;property name=&quot;username&quot; value=&quot;username&quot; /&gt;
              &lt;property name=&quot;password&quot; value=&quot;pswd&quot; /&gt;
  
          &lt;/bean&gt;
      
      &lt;/beans&gt;
     
  &lt;/beans&gt;
  </code></pre>
  <p>As you can see in the previous snippet I am defining 2 different profiles that defines 2 different version of the datasource bean with <code>id=&quot;dataSourcePUC&quot;</code> that create the connection to my 2 different database.</p>
  <p>So, into my <code>main()</code> method I do something like this:</p>
  <pre><code>GenericXmlApplicationContext context = new GenericXmlApplicationContext();
  ConfigurableEnvironment conf = (ConfigurableEnvironment) context.getEnvironment();
  conf.setActiveProfiles(&quot;PROD&quot;); 
  context.load(&quot;applicationContext.xml&quot;);
  context.refresh();
  
  pucManager = (PucManager) context.getBean(&quot;pucManager&quot;);  // my DAO
  
  // QUERY PERFORMED ON THE DB_1:
  List&lt;TassoRendimentoInterno&gt; tassoRendimentoInternoList = pucManager.getTassoRendimentoInterno();
      
  // THIS SECOND QUERY HAVE TO BE PERFORMED ON THE DB_2
  List&lt;TassoInternoRendimentoFondo&gt; tassoInternoRendimentoFondoList = pucManager.getTassoRendimentoInternoFondo();
  </code></pre>
  <p>In this code snippet first I set the active profile on <strong>PROD</strong> so it is used the <strong>DB_1</strong>, in this way:</p>
  <pre><code>ConfigurableEnvironment conf = (ConfigurableEnvironment) context.getEnvironment();
  conf.setActiveProfiles(&quot;PROD&quot;);
  </code></pre>
  <p>It work fine and the query are performed on this DB.</p>
  <p>The problem is that I want to perform the second query on the <strong>DB_2</strong>, so before this line:</p>
  <pre><code>List&lt;TassoInternoRendimentoFondo&gt; tassoInternoRendimentoFondoList = pucManager.getTassoRendimentoInternoFondo();
  </code></pre>
  <p>I have to set the other profile <strong>COLL</strong>.</p>
  <p>To do this operation I also have to stop and refresh the context?</p>
  <p>How can exactly do it? What is the best solution?</p>