JBoss Community

Creating EntityManagers dynamically (e.g. through a config file or database)

created by jaabax in EJB3 - View the full discussion

Hi folks.

Like I asked in the title, how do I create EntityManagers dynamically?

A example is: I have 4 clients using my application (CompanyA, CompanyB, CompanyC, CompanyD). I do not want to create a @PersistenceContext for each one because tomorrow I can add the 5th one and this will require a new deploy and server restart (I want to store clients information on the database).

I've read on Java EE 6 tutorial that I can use @PersistenceUnit to create an EntityManagerFactory and use createEntityManager() afterwards to create the EntityManager (and store the entity managers instances in a map for example).

This is the right way to do it? I'm concerned about concurrency, best practices and etc.

I am using JBoss AS 7 as my application server.

Thanks in advance.

Reply to this message by going to Community

Start a new discussion in EJB3 at Community