1 Reply Latest reply on Dec 3, 2010 5:33 AM by Viggo Navarsete

    Dinamically switch DataSource in EJB3 application

    Sergei Makarov Newbie

      Is there a way to dinamically switch EntityManager's DataSource to working with more then one datanase? For this time I found only way described here:

       

      http://community.jboss.org/message/356772

       

      "bill.burke@jboss.com" wrote:
      deploy your entities as you would but assign them a JNDI name(see docs). In your EJB interceptor pick the appropriate JNDI name of the EntityManager you want to access. Either inject the ENtityManager into the field of your bean using a custom annotation, or stuff the chosen entity manager into a java.lang.ThreadLocal. This way you still get the automatic session management that the EJB/Java Persistence integration gives you. Following me?

        • 1. Re: Dinamically switch DataSource in EJB3 application
          Viggo Navarsete Expert

          Hi,

           

          if you know the JNDI names of the datasources that you want to switch between, you can do it like this:

           

          private Map<String, EntityManager> cachedEntityManagerMap = new HashMap<String, EntityManager>(); // cached EntityManagers

           

           

          private EntityManager getEntityManager() {
          String jndiName = "java:/something/someDS"; // TODO: Here you need to determine the JNDI name of the DataSource you want to switch to!
          log.info( "Change datasource to '" + jndiName + "'" );
          if( cachedEntityManagerMap.containsKey( jndiName ) ) { // if already created, return a cached version instead of creating a new
          log.debug( "Return cached EntityManager with jndiName '" + jndiName + "'" );
          return cachedEntityManagerMap.get( jndiName );
          } else {
          log.debug( "Return new EntityManager with jndiName '" + jndiName + "'" );
          /*
          * See "Hibernate Entity Manager User Guide", chapter "Bootstrapping" for more examples of what to override in
          * the map
          */
          Map<String, String> emMap = new HashMap<String, String>();
          emMap.put( "javax.persistence.jtaDataSource", jndiName );
          EntityManager em = Persistence.createEntityManagerFactory( "somePU", emMap )
          .createEntityManager();
          cachedEntityManagerMap.put( jndiName, em );
          return em;
          }

          private EntityManager getEntityManager() {

          String jndiName = "java:/something/someDS"; // TODO: Here you need to determine the JNDI name of the DataSource you want to switch to!

          log.info( "Change datasource to '" + jndiName + "'" );

           

          if( cachedEntityManagerMap.containsKey( jndiName ) ) { // if already created, return a cached version instead of creating a new

          log.debug( "Return cached EntityManager with jndiName '" + jndiName + "'" );

          return cachedEntityManagerMap.get( jndiName );

          } else {

          log.debug( "Return new EntityManager with jndiName '" + jndiName + "'" );

          /*

          * See "Hibernate Entity Manager User Guide", chapter "Bootstrapping" for more examples of what to override in

          * the map

          */

          Map<String, String> emMap = new HashMap<String, String>();

          emMap.put( "javax.persistence.jtaDataSource", jndiName );

          EntityManager em = Persistence.createEntityManagerFactory( "somePU", emMap )

          .createEntityManager();

          cachedEntityManagerMap.put( jndiName, em );

          return em;

          }

          }