5 Replies Latest reply on May 25, 2010 10:42 AM by richard.qin

    how to use EntityManagerFactory with UserTransaction

    javatwo

      Hello, we are using JBoss 5.1.0.GA

       

      We need to access new datasources on demand. So  we create EntityManagerFactory programmatically

       

      Map configOverrides = new HashMap();


      configOverrides.put("hibernate.connection.url", connectionUrl);
      configOverrides.put("hibernate.connection.username", username);
      configOverrides.put("hibernate.connection.password", password);
             
      EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, configOverrides);

       

      every time

       

      EntityManager em =  emf.createEntityManager()

       

      will create a new session. What we need is that EntityManager is created in the context of UserTransaction.

       

      We can getCurrentSession() from SessionFactory ,but there is not a way to create a EntityManager using the current sesion.

       

      IF we pass a data source (instead of JBDC connection) in configOverrides when creating entityManagerFactory, will it work automatically with UserTransaction? if yes, we can drop a -ds.xml file in deploy directory if a new data source needs to be accessed.

       

      To use UserTransaction , need it be JTA? The EntityManager created above is RESOURCE_LOCAL.

       

      Thanks for help.

      Dave