users@glassfish.java.net

RE: RE: RE: Null Pointer Exception using Connetion class

From: Kenneth Clark <kenneth.clark_at_skyetech.co.za>
Date: Mon, 5 Nov 2007 19:21:46 +0200

Does the connection work the first time and fail on subsequent calls?
 What I might do is have a mechanism in place to return the connection. The method returning the connection then has the ability to check if the connection is null and take appropriate action

public class BuildCms {
  DataSource db;
  
  public BuildCms() throws NamingException, SQLException {
    db = BuildCms.getJdbcPwacms();
  }

  public DataSource getJdbcPwacms() throws NamingException {
        Context c = new InitialContext();
        return (DataSource) c.lookup("XXXXXXXXXXXXXX");
  }

  private Connection getMyConnection(){
    if(db.getConnection() == null){
      Context c = new InitialContext();
      db = (DataSource) c.lookup("XXXXXXXXXXXXXX");
    }
    return db.getConnection();
  }

  public String getCMSPage(String page) throws NamingException, SQLException {
      
    Statement state = this.getMyConnection().createStatement();


Sorry if I am wrong I am assuming the db.getConnection() was throwing the null error. The other thing to check is are you closing the connection anywhere? Closing a resultset that is bubbling to close the connection?
________________
Thanks and regards

Kenneth Clark
Solutions Engineer


Tel: 27 11 679 3075
Mobile: 27 (0) 84 583 1348
Email: kenneth.clark_at_skyetech.co.za
Website: http://www.skyetech.co.za


-----Original Message-----
From: glassfish_at_javadesktop.org [mailto:glassfish_at_javadesktop.org]
Sent: 05 November 2007 19:08
To: users_at_glassfish.dev.java.net
Subject: Re: RE: RE: Null Pointer Exception using Connetion class

Thanks - the thing about this that puzzles me is that it only give the nullpointer exception while conn is defined as static. If I change to the following code and then (tediously and wastefully) insantiate a BuildCms object from the servlet it all works fine. .... Any ideas why?

[code]
public class BuildCms {
  
    public DataSource getJdbcPwacms() throws NamingException {
        Context c = new InitialContext();
        return (DataSource) c.lookup("java:comp/env/jdbc/pwacms");
    }
    
    public String getCMSPage(String page) throws NamingException, SQLException {
        DataSource db = this.getJdbcPwacms();
        
        Statement state = db.getConnection().createStatement();
        ResultSet results;
[/code]
[Message sent by forum member 'hughacland' (hughacland)]

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

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
For additional commands, e-mail: users-help_at_glassfish.dev.java.net

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.503 / Virus Database: 269.15.22/1111 - Release Date: 2007/11/05 04:36
 

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.503 / Virus Database: 269.15.22/1111 - Release Date: 2007/11/05 04:36