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.
views:
168answers:
3
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
2010-03-01 06:37:19
+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
2010-03-01 08:34:50
A:
See my answer to the related question http://stackoverflow.com/questions/2311614/how-do-i-connect-to-multiple-databases-using-jpa/2311663#2311663
Roman
2010-03-01 08:51:07