开发者

Using Spring Framework is it possible to connect two different databases based on some business logic

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 t开发者_开发百科he database configuration is done through JNDIName entry in an xml file.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜