Search code examples
jdbcconnection-poolingglassfish-4.1

Get a jdbc connection from pool created in glassfish 4


I created in glassfish4.1 console a new connection pool. I named it "MyPool" and successfully pinged. Now I expect to use in my web-app something like:

java.sql.Connection connection = dontknow.getFromPool("MyPool");

but I did not find any example on how to retrieve it. Is there any sample to copy from?


Solution

  • The connection pool must be linked ad jdbc resource, namely "jdbc/MyResource". Then:

    javax.naming.InitialContext ctx = new javax.naming.InitialContext();
    DataSource ds = (DataSource) ctx.lookup("jdbc/MyResource");
    Connection conn = ds.getConnection();