title: How to programmatically change databases in Spring with one DataSource?
body: <p>I'm looking to see what's the best way to use one DataSources in Spring but be able to switch the database from within the Java code?  Below are my two DataSources and they go to the same database server but different databases.  </p>
  
  <pre><code>&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close"&gt;
      &lt;property name="driverClassName" value="com.sybase.jdbc3.jdbc.SybDataSource" /&gt;
      &lt;property name="url"
                value="jdbc:sybase:Tds:10.20.30.40:50/DATABASE_EMS" /&gt;
      &lt;property name="username" value="userid" /&gt;
      &lt;property name="password" value="derp" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSourceMain" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close"&gt;
      &lt;property name="driverClassName" value="com.sybase.jdbc3.jdbc.SybDataSource" /&gt;
      &lt;property name="url"
                value="jdbc:sybase:Tds:10.20.30.40:50/DATABASE" /&gt;
      &lt;property name="username" value="userid" /&gt;
      &lt;property name="password" value="derp" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>I have them bound to their own respective bean but I'm looking at my legacy code and it's going to be VERY awkward to implement this with 2 separate beans.  Is there any ideas/thoughts on how to be able to use one DataSource and switch databases when I need to?</p>