Reputation: 21
I am working on a Spring/Hibernate application that allows users to connect to a web site, create, work on, and save their own projects. Each project, whose name is defined by the user in real time, will be an SQL database on the server. The idea is to have an "application" database, where user's details and association between user and his projects (databases) are stored, and several "project" databases, dynamically created and updated by the user.
So I would like Spring to dynamically create the new database when the user creates a new project, or address and work with an existing database when the user open and existing project, and so on. Of course, this is something that cannot be stored in xml configuration files.
At the moment I implemented a prototype with a routing class for entity managers ( like explained in https://spring.io/blog/2007/01/23/dynamic-datasource-routing/) and I am able to switch between two datasources as I defined in servlet.context.xml.
Now I would like to dinamically add other entries to the datasource bean and have the possibility to switch in real time between them depending on which project the user is currently editing.
<bean id="dataSource" class="com.myapp.spring.config.DbConnectionRoutingDataSource">
<property name="targetDataSources">
<map key-type="com.myapp.spring.config.ContextType">
<!-- ... I would like to populate and manage this dynamically ... -->
<entry key="application" value-ref="applicationDataSource"/>
<entry key="project" value-ref="projectDataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="applicationDataSource" />
</bean>
I can't find documentation around about doing this, so I have some concerns and questions.
1) I found an object called "BeanFactoryPostProcessor": is it suitable for this purpose? Is it possible to use it anytime/everywhere in the code? Documentation around about using this is very poor.
3) Spring will automatically reconfigure itself when the datasource configuration changes? I mean, when I add a new datasource the existing datasources will still work or I will go throw a kind of "reset"?
4) This is more an alternative idea: Instead of adding many datasource values, could I just reconfigure dynamically the DriverManagerDataSource for projects (by changing the database name in the URL property) and use the same datamanager? With this approach I would only have two datasources ("application" and "project"), but the one for projects would change the target database whereby the user is actually dealing with.
<bean id="projectDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
Thank you very much for answering.
P.
Upvotes: 2
Views: 1571
Reputation: 355
I know this is pretty old question but for the sake of others who come across this question like me, I thought I would post my answer here.
As Kayaman already mentioned, i think you need to implement a multi-tenant application. I'm posting here a sample example:
https://javadeveloperzone.com/hibernate/spring-hibernate-xml-multi-tenancy-example/
Upvotes: 1