users@glassfish.java.net

Dynamically bind a datasource to a persistence unit ?

From: <glassfish_at_javadesktop.org>
Date: Thu, 26 Jul 2007 05:13:32 PDT

Hi all,

I'm trying to define a persistence unit using a set of properties :

<persistence-unit name="PU_4" transaction-type="JTA">

                  <provider>
                       oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
                  </provider>
                  
                  <properties>
                     <property name="toplink.jdbc.url" value="jdbc:oracle:thin:@londres:1521:OCP"/>
                     <property name="toplink.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/>
                     <property name="toplink.jdbc.user" value="tdandelo"/>
                     <property name="toplink.jdbc.password" value="tdandelo"/>
                  </properties>
        </persistence-unit>
        
        Because we have a large set of datasources which may vary during the application's life, my goal is to dynamically define a datasource into the code for a given persistence unit
        
        The final goal is to remove the "url", "user" and "password" properties to dynamically define them into the invocation code :
        
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence/PU_4");
                                        
        Map args = new HashMap() ;
                                        
        args.put("toplink.jdbc.user", "xxx") ;
        args.put("toplink.jdbc.password", "xxx") ;
        args.put("toplink.jdbc.url", "jdbc:oracle:thin:@xxx:1521:xxx") ;
                                        
        EntityManager em = emf.createEntityManager(args) ;
        
        All this sounds great... but I currently have the following error :
        
        javax.ejb.EJBException: nested exception is: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
        java.rmi.RemoteException: null; nested exception is:
        javax.persistence.PersistenceException: No Persistence provider for EntityManager named persistence/PU_4
        
        I don't understand why I get this error, since the persistence provider is a fixed property into the persistence unit conf.
        
        Could someone tell me
        
        1) If I am in the right way to do what I explained ... ?
        
        2) Why I get this error ? :)
        
        Thanks in advance !
[Message sent by forum member 'olivier_g' (olivier_g)]

http://forums.java.net/jive/thread.jspa?messageID=228331