0 Replies Latest reply on May 2, 2007 11:56 AM by riddick

    Can't use Persistence.createEntityManagerFactory to dynamica

    riddick

      Hi there,

      I'm trying to build an example where the datasource depends on the user. Only the database username and password can change.
      I'd like to create an entityManagerFactory with a map containing the username and password and thus using an appropriate entitymanager but it fails.

      Here is the code of my session ejb

      @Stateless
      @Local(value = { ContactManager.class })
      @Remote(value = { RemoteContactManager.class })
      
      public class ContactManagerBean implements ContactManager,RemoteContactManager {
      
       /**
       * Ajoute un contact
       */
       public ContactDTO addContact(ContactDTO contactDTO) {
       Map<String, String> env = new HashMap<String, String>();
       env.put("hibernate.connection.username", "test");
       env.put("hibernate.connection.password", "test");
      
       EntityManagerFactory emf =Persistence.createEntityManagerFactory("testUnit",env);
      
      
       EntityManager em = emf.createEntityManager();
       Contact contact = ContactAdapter.getContact(contactDTO);
       EntityTransaction tx = em.getTransaction();
       tx.begin();
       em.persist(contact);
       tx.commit();
       em.close();
       return ContactAdapter.getContactDTO(contact);
       }
      
       /**
       * Liste l'ensemble des contacts de la base de données
       */
       public Collection<ContactDTO> listContact() {
       EntityManagerFactory emf =Persistence.createEntityManagerFactory("testUnit");
       EntityManager em = emf.createEntityManager();
       EntityTransaction tx = em.getTransaction();
       tx.begin();
       Collection<Contact> colC= em.createQuery("SELECT c FROM Contact c").getResultList();
       tx.commit();
       em.close();
       return ContactAdapter.getContactDTOCollection(colC);
       }
      }
      

      Here is my persistence.xml file:
       <persistence-unit name="testUnit" transaction-type="JTA">
       <jta-data-source>java:TestDS</jta-data-source>
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <class>entity.Contact</class>
       <properties>
       <property name="hibernate.hbm2ddl.auto" value="validate" />
       <property name="hibernate.dialect"
       value="org.hibernate.dialect.MySQLInnoDBDialect" />
       <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test" />
       <property name="hibernate.connection.username" value="sa" />
       <property name="hibernate.connection.password" value="" />
       <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
       <property name="hibernate.connection.pool_size" value="10" />
       </properties>
       </persistence-unit>
      

      And here is the error I get

      ...
      Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
       at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:698)
       at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
       at session.bean.ContactManagerBean.listContact(ContactManagerBean.java:72)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
       at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
       at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:46)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
       ... 81 more
      Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
       at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:329)
       at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1218)
       at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:691)
      


      Can anyone can explain me how to deal with the transaction strategy?
      Is there a better way to dynamically change the database user?

      Thanks by advance,

      Anthony