users@glassfish.java.net

Creating EntityManager with a specific username and password?

From: <glassfish_at_javadesktop.org>
Date: Mon, 29 Dec 2008 10:37:14 PST

Is there a way to pass a username/password to the javax.persistence.EntityManagerFactory to create a javax.persistence.EntityManager with that username/password rather than the username/password supplied by default when creating the connection pool under the Glassfish Server Admin Console?

For example with the javax.sql.DataSource class there is a
ds.getConnection(username,password) that returns a connection with that username/password (i.e. permissions are handled by the database), is there an equivalent for EntityManager?

For example, suppose I create the following EJB to just execute an SQL statement:

@Stateful
public class SomeEJB implements SomeInterface
{
     @PersistenceUnit (name="common")
      private java.persistence.EntityManagerFactory emf;
   
      private Logger log = Logger.getLogger("test");


      public void executeSQL(String sql)
        {
                java.persistence.EntityManager em=null;
                try
                {
                        
                        java.util.Properties map = new Properties();
                        map.put("toplink.jdbc.user", username);
                        map.put("toplink.jdbc.password", password);

                        em=emf.createEntityManager(map);
                        Query query=em.createNativeQuery(sql);
                        query.executeUpdate();
                }
                catch (Exception e)
                {
                        log.fatal(e.getMessage(),e);
                }
                if (em!=null){em.close();}
        }
}

What Map do I insert to the EntityManagerFactory?

Thank you very much for any assistance.
[Message sent by forum member 'enderfake' (enderfake)]

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