0 Replies Latest reply on Jun 4, 2012 8:44 PM by jaabax

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

    jaabax Newbie

      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 new EntityManager instance variable annotated with @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 HashMap for example).

      This is the right way to do it? It's a good idea to me to control the EntityManagers creation? I'm concerned about concurrency, best practices and etc.

      I am using JBoss AS 7 as my application server.

      Thanks in advance.