I have an application using Spring MVC and Spring Security.
The application project has ejbs and web application.
I have a common database for users and one database by users’ group.
So, when a user authenticate, Spring Security use the common database to check login/password and when a controller calls an ejb, the entity manager connects to the user’s group database.
My goal is : create users/groups and their associated database on the fly.
Example :
The common database localhost:3306 database_schema1 contains
USERS
----------------------
| NAME | GROUP_ID |
----------------------
| Pierre | 1 |
| Paul | 1 |
| Jacques | 2 |
----------------------
GROUPS
------------------------------------------
| ID | URL | SCHEMA_NAME |
------------------------------------------
| 1 | localhost:3307 | database_schema1 |
| 2 | localhost:3308 | database_schema1 |
------------------------------------------
When Pierre authenticate, the home page call an ejb. This ejb has an entity manager which has to connects to group 1 database localhost:3307 with schema database_schema1
If Jacques authenticate, it must be group 2 database.
How can I do to tell to the entity manager to use a given database that I don’t know when I build the project?
I mean, I don’t want to use persistence.xml in my ejb but dynamically initialize entity manager depending on the user authenticated.
Does Persistence.createEntityManagerFactory() will work in this context ? And if it does, is it the best solution?
You can do this by removing the database URL from
persistence.xmland specifying it at runtime:Then, use this
factoryobject to createEntityManagerinstances as needed.