JPA/Eclipselink - select DataSource/JDBC URL dynamically
Is there a way that I can get JPA (Eclipselink specifically) to dynamically select a connection pool depending on some property bound to the current thread?
The problem I'm trying to solve is scaling a multitenant system, where tenants may be split up over several DB instances. Each DB instance will be multitenant, but to scale, I may not be able to fit all tenants in one DB instance comfortably.
I am familiar with @Multitenant to support single-table multitenancy, and have successfully used Eclipselink session event callbacks to set values in the Eclipselink Session dynamically. I'm trying to take the next step of changing the DataSource that the EntityManager will be using, so I don't have to necessarily use a clustered DB.
Take a look at this
It creates a datasource that loadbalances to backend datasources.
You can use an EntitiyManagerFactory which allows the creation of an EntitiyManager for a specific datsasource (via a String property) as explained under
You might want to check this post. It might be helpful or give you a clue on how to achieve your solution.