users@glassfish.java.net

Re: Creating EntityManager with a specific username and password?

From: <glassfish_at_javadesktop.org>
Date: Mon, 05 Jan 2009 08:52:03 PST

Thank you very much for the help. That does indeed seem to be the only way to change the username/password, by creating the the persistence unit via a non-standardized approach, rather than :
<jta-data-source>jdbc/common</jta-data-source> (in the persistence.xml)
Glassfish Connection pool.

So this works:

public void executeSQL(String sql)
{
java.persistence.EntityManager em=null;
try
{

                        java.util.Properties map = new Properties();
                        map.put("toplink.jdbc.driver", "database-driver");
                        map.put("toplink.jdbc.url", "database-url");
                        map.put("toplink.jdbc.user", "username");
                        map.put("toplink.jdbc.password", "password");
                        
                        EntityManagerFactory test=Persistence.createEntityManagerFactory("common", map);

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


For future reference, this seems to be a limitation of the Toplink Essentials JPA that Glassfish V2 uses, per the following:
http://www.oracle.com/technology/products/ias/toplink/JPA/essentials/toplink-jpa-extensions.html#TopLinkJDBC

------- Note ----------
You may specify any TopLink JPA extension in a persistence.xml file and you may pass any TopLink JPA extension into Persistence method createEntityManagerFactory.

Currently, no TopLink JPA extensions are applicable to EntityManagerFactory method createEntityManager.

Hopefully EclipseLink (Toplink's replacement) in Glassfish V3 won't have this problem.

Thank you again cigorin for your help, it was much appreciated.
[Message sent by forum member 'enderfake' (enderfake)]

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