This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Dynamic Datasource Routing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Dynamic Datasource Routing" Watch "Dynamic Datasource Routing" New topic

Dynamic Datasource Routing

Jans Daniels

Joined: Jun 28, 2011
Posts: 3

Hi, i'm stuck with this:
Our application is going to be a plain EJB app with SAP JPA Persistence (arrgh...)

What I'm trying to do is to switch the EntityManager's underlying datasource during application runtime.
I tried to implement my own DataSource (as SLSB), which does the lookup on the 'real' DataSources.
This works so far, but now I have to inject that custom DataSource into my Persistencecontext somehow.

Hope someone of you has some ideas for me,
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
Why are you trying to do this?

Do you want to change it once on startup, or per query/transaction?

If you are trying to do some kind of dynamic data partitioning, you may wish to look at EclipseLink's data partitioning, replication and load balancing support,

Otherwise, you may wish to use an application managed EntityManager/Factory instead of container managed. Then you have control of its creation and can pass the data source in the properties map.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Jans Daniels

Joined: Jun 28, 2011
Posts: 3

Hi, thanks for your quick reply.

No, I do not want to partition my data, I'd like to be able to change the DataSource per query.
The use case is: User logs on to web, gets a combobox with all available Datasources displayed
and chooses one. The user must be able to switch to another datasource during his session.

Meanwhile I found something which contains a solution I will try tomorrow.
It's sort of an application managed solution, but I thought of something more elegant, like a dynamic DataSource implementation that
will be fed to an EntityManager or EntityManagerFactory...

Thanks fo your help,


jQuery in Action, 3rd edition
subject: Dynamic Datasource Routing
It's not a secret anymore!