webtier@glassfish.java.net

Re: Glassfish 4.0 using jdbc/__default only

From: <forums_at_java.net>
Date: Sun, 28 Jul 2013 09:56:21 -0500 (CDT)

Kuzmit, I finally got my GF4 DataSource working. I all honesty, I do not know
precisely which change did the trick, of if it was a combination of the
changes that did it. Here's what I did: 1. Updated the latest PostgreSQL
Driver 2. Changed from using the PGConnectionPoolDataSource to the
PGSimpleDataSource 3. Deleted the Derby connection pool and jdbc/__default
resources from my domain.xml 4. Reworked my Database getConnection() method
5. Moved my Connection Pool and JDBC Resource config into the the java:app
JNDI namespace. Here is my getConnection() method. (It is still a bit messy
from the whole debugging process): private static String jndiName = null;
private static InitialContext initCtx = null; private static Context
appContext = null; private static DataSource dataSource = null; public static
Connection getConnection() throws SQLException { Logger logger =
Logger.getLogger(DBConnection.class); Connection con = null; Object jndiRes =
null; try { // // Retrieve an return a connection from the JNDI connection
pool. // if (initCtx == null) { logger.info("Creating Initial Context...");
initCtx = new InitialContext(); } // // Retrieve the JNDI Context for the
application resources. // if (appContext == null) { logger.info("Looking up
java:app/..."); appContext = (Context) initCtx.lookup("java:app/"); if
(appContext == null) { logger.error("ERROR!!! Null ENV Context!"); throw new
SQLException("Lookup on java:app/ Failed!"); } } // // Lookup the Datasource
from the APPLICATION context. // if (dataSource == null) {
logger.info(String.format("Retrieving DataSource (%s)...", jndiName));
jndiRes = appContext.lookup(jndiName); if (jndiRes == null) {
logger.error("ERROR!!! Null Context Lookup!"); throw new
SQLException("Datasource not found"); } else { if (jndiRes instanceof
DataSource) { dataSource = (DataSource)jndiRes; } else { SQLException ex =
new SQLException(errorMsg); logger.fatal(errorMsg, ex); throw ex; } } } // //
Get the connection from the DataSource // logger.info("Retrieving
Connecton..."); con = dataSource.getConnection(); if (con == null) {
logger.error("ERROR!!! Null Connection!\n"); throw new SQLException("Could
not get connection"); } } catch (NamingException e) { logger.error("ERROR!!!
Caught NamingException!",e); throw new SQLException(e.getMessage()); } catch
(SQLException sqle) { logger.error("ERROR!!! Caught SQLException!", sqle);
throw sqle; } return con; } getConnection() method was NOT new. I first wrote
it several years ago and it has been essentially unchanged since then. I was
quite surprised when the code started having problems. My suspicion is that
GF4 has made changes to the JNDI lookup and naming classes, but that is just
a guess. I hope this helps. Mike Laris mglaris_at_gmail.com

--
[Message sent by forum member 'mlaris']
View Post: http://forums.java.net/node/897508