I have an uncommon requirement in my web application. It is a normal tomcat, spring, hibernate application, but each user want to select the DB to work with directly after login. After a successful login the user get a drop down list with available Databases, selects one and continue using the application. All Databases have the same schema, only different data and the connection data are stored in a properties file.
I think i have to create an EntityManager for each user after login. I am wondering, what is the best practice to do so. May be it also makes sense, that when two users selects the same DB, that they share the EntityManager (e.g. A check whether an EntityManager for this DB already exists before creating a new one). I also want to use connection-pooling per EntityManager
Concrete questions
- Is there a best practice?
- Which EntityManagerFactory (see below) is suitable, the container-based or the standalone-one?
- Is there any risks, like memory leakage, performance etc.?
So far I checked the following classes from spring:
- LocalEntityManagerFactoryBean
- LocalContainerEntityManagerFactoryBean
This is also a link that creates EntityManager programatically but with spring classes https://vladmihalcea.com/how-to-bootstrap-jpa-programmatically-without-the-persistence-xml-configuration-file/