users@glassfish.java.net

And yet another Websphere 5.x to Glassfish App Conversion - JDBC naming

From: <glassfish_at_javadesktop.org>
Date: Thu, 13 Nov 2008 11:20:40 PST

Once again, I am running into another issue. I have code that is looking for a specific name for a JDBC object that does not seem to be within the Context of the APP. And looking around at the resources, I don't see the name in specific properties page either. This is strange and makes no sense to me - how and why would this work in Websphere?

Here's the problematic code:<BR><BR>[code]
 protected Connection getConnection(PropertiesManager manager)
   throws Exception, SQLException
  {
    Connection con = null;
    if(DEBUG) System.out.println("BaseDatabaseBean.getConnection(PropertyManager object) - calls JDBCPingTables.setTables method using object");
    // sets up an object containing table name
    JDBCPingTables.setTables(manager.getProperties());
    if(DEBUG) System.out.println("BaseDatabaseBean.getConnection(PropertyManager object) - creates new JDBCPingHandler object");
     // a simple Transaction handler object (used to determine connection to a DB Table
     // and provide definitive assurance for insert&read followed by rollback)
    JDBCPingHandler pingHandler = new JDBCPingHandler();
    // Applying Properties
    String dsname = manager.getString(PROP_DATASOURCE, DEFAULT_DATASOURCE);
    String userid = manager.getString(PROP_DATABASE_USERID, DEFAULT_DATABASE_USERID);
    String password = manager.getString(PROP_DATABASE_PASSWORD, DEFAULT_DATABASE_PASSWORD);
    int attempts = manager.getInt(PROP_DATABASE_ATTEMPTS, DEFAULT_DATABASE_ATTEMPTS);
    boolean manageTransactions = manager.getBoolean(PROP_MANAGE_TRANSACTIONS, DEFAULT_MANAGE_TRANSACTIONS);
    // Verify
    if(DEBUG) System.out.println("BaseDatabaseBean.getConnection(PropertyManager object) - sets vars from object: str dsname= "+dsname);
    if(DEBUG) System.out.println(" str userid= "+userid);
    if(DEBUG) System.out.println(" str password= "+password);
    if(DEBUG) System.out.println(" int attempts= "+attempts);
    if(DEBUG) System.out.println(" bool manageTransactions= "+manageTransactions);
    // first time through, we have no datasource yet
    if(m_DataSource == null)
    {
      try
      {
        // first time through, properties file "DATASOURCE" does not start with 'jdbc/'
        if(!dsname.startsWith("jdbc/"))
        {
          dsname = "jdbc/"+dsname;
        }
          
        if(DEBUG) System.out.println("BaseDatabaseBean.getConnection(PropertyManager object) - calls getEnv passing dsname= "+dsname+" to create a DataSource obj: m_DataSource");

        // ==> HERE'S THE PROBLEM, getEnv can't find datasource name!!! <==
        // getEnv calls a method that returns a result of InitialContext.Lookup(dsname)
         m_DataSource = (DataSource) getEnv(dsname);
     }
      catch(Exception e)
      {
        m_DataSource = null;
        errorLog("BaseDatabaseBean.getConnection() - Exception: "+e);
        throw e;
      }
    }
    
    if(m_DataSource != null)
    {
      for(int i = 0; con == null && i < attempts; i++)
      {
            if(DEBUG) System.out.println("BaseDatabaseBean.getConnection(PropertyManager object) - calls m_DataSource.getConnection passing userid and password "+i+" of "+attempts+" times");
        con = m_DataSource.getConnection(userid, password);
        
        if(con != null)
        {
         if(DEBUG) System.out.println("BaseDatabaseBean.getConnection(PropertyManager object) - calls pingHandler.ping(connectionObj) to determine we can reach it");
         if(pingHandler.ping(con) == null)
          {
            try{ con.close(); }catch(Exception e1){}
            con = null;
            if(DEBUG) System.out.println("BaseDatabaseBean.getConnection(PropertyManager object) - calls to pingHandler.ping(connectionObj) unable to reach it");
          }
        }
      }

      if(con != null && manageTransactions)
      {
        if(DEBUG) System.out.println("BaseDatabaseBean.getConnection(PropertyManager object) - calls to pingHandler.ping(connectionObj) successful");
        try
        {
          con.rollback();
          con.setAutoCommit(false);
        }
        catch(Exception e)
        {
          errorLog("BaseDatabaseBean.getConnection() - Exception: "+e);
          throw e;
        }
      }
    }
    else
    {
      errorLog("BaseDatabaseBean.getConnection() - DataSource '"+dsname+"' is null.");
    }
      
    if(DEBUG) System.out.println("BaseDatabaseBean.getConnection(PropertyManager object) - returning connectionObject");
    return con;
  }
}[/code]<BR><BR>
And the LOG:<BR>[code]
[#|2008-11-13T12:58:36.326-0600|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=14;_ThreadName=httpSSLWorkerThread-8081-1;|
BaseDatabaseBean.getConnection(PropertyManager object) - creates new JDBCPingHandler object|#]

[#|2008-11-13T12:58:36.326-0600|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=14;_ThreadName=httpSSLWorkerThread-8081-1;|
BaseDatabaseBean.getConnection(PropertyManager object) - sets vars from object: str dsname= CBCWEB|#]

[#|2008-11-13T12:58:36.326-0600|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=14;_ThreadName=httpSSLWorkerThread-8081-1;|
                                                                                 str userid= cbcweb|#]

[#|2008-11-13T12:58:36.326-0600|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=14;_ThreadName=httpSSLWorkerThread-8081-1;|
                                                                                 str password= cbcweb|#]

[#|2008-11-13T12:58:36.326-0600|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=14;_ThreadName=httpSSLWorkerThread-8081-1;|
                                                                                 int attempts= 5|#]

[#|2008-11-13T12:58:36.326-0600|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=14;_ThreadName=httpSSLWorkerThread-8081-1;|
                                                                                 bool manageTransactions= true|#]

[#|2008-11-13T12:58:36.342-0600|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=14;_ThreadName=httpSSLWorkerThread-8081-1;|
BaseDatabaseBean.getConnection(PropertyManager object) - calls getEnv passing dsname= jdbc/CBCWEB to create a DataSource obj: m_DataSource|#]

[#|2008-11-13T12:58:36.342-0600|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=14;_ThreadName=httpSSLWorkerThread-8081-1;|
BaseBean.getEnv() - method START with passed string: jdbc/CBCWEB|#]

[#|2008-11-13T12:58:36.342-0600|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=14;_ThreadName=httpSSLWorkerThread-8081-1;|
BaseBean.getEnv() - lookup ENV name and return|#]

[#|2008-11-13T12:58:36.342-0600|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=14;_ThreadName=httpSSLWorkerThread-8081-1;|
BaseBean.getEnv() - ENV LOOKUP FAILED|#]

[#|2008-11-13T12:58:36.342-0600|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=14;_ThreadName=httpSSLWorkerThread-8081-1;|
11/13/2008 12:58:36 - BaseBean.getEnv() - Exception: javax.naming.NameNotFoundException: CBCWEB not found|#][/code]<BR><BR>
What's even more troubling, we've already created a JDBC Connection Pool and retrieved some data earlier during an INIT phase of the application's HTTPServlet before any beans were created. Yet, in this Websphere code, the developers make no use of the established Pool - nevertheless, it also tells me that since we've already made a connection to (and retrieved data from) the database, there already is something in the environment - though definitely nothing starting with "jdbc/<datasourceName>". Looking at the properties file for the bean only shows the name of the database and there is nowhere anything resembling some sort of jndi binding of that name for a call to InitialContext.lookup() to find. Looking at the ejb-jar.xml only shows the bean info and does not offer any JNDI binding for a datasource. This begs a question as to how this code works for Websphere...<BR><BR>
Next question - how do I create a datasource connection that will work? The earlier JDBC connection creating the ConnectionPool used the DriverManager.getConnection() method passing in ServerName, Username, and Password - not a datasource (Database) name. Totally different type connection...
<BR><BR>da Lizard
[Message sent by forum member 'loungelizard' (loungelizard)]

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