0

I have an application using JSF,EJB,JPA, Glassfish. There are multiple(may be around thousand or more) clients using my app, however each client has a separate database. All the databases have the same schema. I would like to determine which database connection to use at the time when a user logs into the system.

For example client A enters client code, username, and password and logs in, I determine that client A belongs to database A, grab the connection for database A and continue on my merry way.

I am using JPA as my persistence provider. Is it possible to set datasource in persistence.xml at runtime? Is there .java version of persistence.xml available? Is there a better/preferred way to do this? PersistenceUnit name will be same for all the connections.

Thanks

paawan
  • 111
  • 1
  • 2
  • 13

1 Answers1

0

If you know all users in advance then you can create separate persistence unit for every user in persistence.xml and use factory for select Persistence Unit Name for users. Addition of users, require modification in persistence.xml and redeployment of Application.

Manish Kumar
  • 133
  • 14
  • I do not know all users in advance. Can you provide me example link? – paawan Jul 30 '20 at 06:23
  • Is there another method available rather than creating separate persistence unit for every user? – paawan Jul 30 '20 at 16:33
  • Your every user have separate database, So no point of having one persistence unit. Simply think of situation, where multiple user concurrently using your system. – Manish Kumar Jul 31 '20 at 09:07
  • Can't we use something like multitenant? https://www.toptal.com/hibernate/build-multitenant-java-hibernate – paawan Aug 15 '20 at 14:45