users@glassfish.java.net

Re: Per Application User Connection Pool Configuration for Auditing

From: <glassfish_at_javadesktop.org>
Date: Wed, 18 Nov 2009 09:44:08 PST

I'm going to guess that if you want Glassfish managing the connection pools rather than your application code, then you will be stuck with a single connection pool (probably set up with a dummy user that has access to nothing). Then you supply the username/password when creating the connection.

If you want to create connections using the credentials supplied to the @Stateful? EJB's do the following....

* First and foremost, when creating the connection pool via the Glassfish Admin page, make sure to click on Advanced Options, and check Match Connections. This matches up the connections by EJB (Thread?), otherwise you're pulling from the pool some random connection, which may have some different username/credentials ....

* Then just do the normal :
@Resource(name="jdbc/your_jndi_name", type=javax.sql.DataSource.class)
private DataSource ds;
private Connection con;
public void someFunction()
{
   try
   {
        con=ds.getConnection(YOUR_USERNAME,YOUR_PASSWORD);
         /// do something with connection
   }
   catch (Exception e){}
   finally
   {
     try
     {
         con.close();
     }
     catch (Exception e){}
   }
}

You'll have to use @Stateful EJB's if you want to keep the connection between EJB function calls.

Otherwise you are using the EntityManager for persistence/database access.... so do the following..
Properties login = new Properties();
login.put(org.eclipse.persistence.config.PersistenceUnitProperties.JDBC_USER, username);
login.put(org.eclipse.persistence.config.PersistenceUnitProperties.JDBC_PASSWORD, password);
EntityManagerFactory emf_residata=Persistence.createEntityManagerFactory("persistence_unit",login);
EntityManager em = emf_ticostat.createEntityManager();

I would highly recommed using eclipselink by adding it to your project and then add the following to your persistence.xml file:
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

I was never able to get the default persistence provider Toplink to actually use the credentials when creating EntityManagerFactory's.

Hope this helps.
[Message sent by forum member 'enderfake' ]

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