1 Reply Latest reply on Mar 6, 2009 1:25 PM by jobbyjoseph

    Dynamic data sources with EntityManager

    wiberto

      I have an application where the data source it uses is saved in the database. This is because each user can have its own data source.

      So UserA will have an entry in it's user information table that says to use data source jdbc/UserA.

      Lets assume that this entry is already configured in the app server itself.

      How do I tell the EntityManager at runtime to use that datasource.

      If this was a static configuration I know I will have the persistence unit defined in my persistence.xml file and I would set the annotation in my entity. Something like this

      @PersistenceContext(unitName="userAunit")
      private EntityManager em;
      


      So how do I make this generic? Do I need to override any EntityManager API to be able to select the unitName at run time? Also, can I create persistence units on the fly?

      Thanks in advanced,

      Jose