0

I have a login page where I have to select which database it should connect enter image description here

I have my configuration like this:

 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" />
  <property name="url" value="jdbc:db2://my_server:10000/DATABASE_1" />
  <property name="username" value="galadm" />
  <property name="password" value="galadm" />
 </bean>

I using Spring JDBC Template Can I write something like this

<property name="url" value="jdbc:db2://my_server:50000/DATABASE{database_which_I_get_from_input}" />

I don't mind to have initial value i.e. DATABASE_1

SK.
  • 1,390
  • 2
  • 28
  • 59

1 Answers1

1

Seems that the AbstractRoutingDataSource is a viable solution for you. Its the layer that acts as an intermediary between the multiple datasource, and determines which one to use dynamically.

The following blog solution describes how you can switch based on some attribute of the user’s context

https://spring.io/blog/2007/01/23/dynamic-datasource-routing/

Master Slave
  • 27,771
  • 4
  • 57
  • 55
  • +1. Superb, I was banging my head for 2 days. Thanks a lot! Just one more question. It looks like AbstractRoutingDataSource is deprecated. Is there any other way? – SK. Mar 31 '15 at 23:41
  • This is the most standard way when you're already using spring in the stack. Of course, there are other ways, you can look for [multitenancy](http://blog.trixi.cz/2012/01/multitenancy-using-spring-and-postgresql/) or provide your own custom impl. But, afaik, AbstractRoutingDataSource, is around for a long time, but, its not deprecated – Master Slave Apr 01 '15 at 05:12
  • I am sorry, I typed wrong. it SimpleJdbcDaoSupport which is deprecated. – SK. Apr 01 '15 at 13:41
  • no worries, do report back if you ended up using the approach, best – Master Slave Apr 01 '15 at 13:43
  • It worked perfectly. Thank You. [Here](http://howtodoinjava.com/2013/12/28/spring-3-2-5-abstractroutingdatasource-example/) is a complete example with source code. – SK. Apr 01 '15 at 18:20