tags:

views:

168

answers:

3

I have a web application which connects to an Oracle database. The application is now going to have a new set of users. A new db is being planned for this new set of users. Is it possible to connect to the appropriate db based on the user who logs in. As of now the database configuration is done through JNDIName entry in an xml file.

A: 

I'd suggest injecting both the data sources into your DAOs and then within your DAO decide the correct data source to use based on the current user. The current user can be passed to the DAO from your presentation/service layer.

Chandru
+1  A: 

Absolutely. For a given DAO class (assuming you're using DAOs), create two bean definitions, one for each database, and then pick which DAO bean you want to use in your business logic:

<bean id="dao1" class="com.app.MyDaoClass">
   <property name="dataSource" ref="dataSource1"/>
</bean>

<bean id="dao2" class="com.app.MyDaoClass">
   <property name="dataSource" ref="dataSource2"/>
</bean>

Where dao1 and dao2 are the DataSource beans representing your two different databases.

At runtime, your business logic selects dao1 or dao2 appropriately.

skaffman