From: David Johnston <polobo@yahoo.com>
To: Benjamin.Galaviz@LSGSkyChefs.com
Cc: pgsql-jdbc@postgresql.org
Subject: Re: PGPoolingDataSource problem.
Date: Wed, 8 Jun 2011 14:32:55 -0400
Message-ID: <02f801cc260a$7c0df640$7429e2c0$@yahoo.com> (raw)
In-Reply-To: <E2435754E1952542B9E77BDDC3E83942076D9EA7@sw-adclsg-mbx01.americas.lsgsc.com>
References: <E2435754E1952542B9E77BDDC3E83942076D9EA7@sw-adclsg-mbx01.americas.lsgsc.com>
You have two major programming issues:
1) You ignore exceptions
2) You use static variables / singleton pattern without understanding
Get yourself a good debugger, where you can step through the code
line-by-line, and watch what happens to various "connection" variables
during your two procedures. It will be obvious why and where you "released"
the original connection without closing it.
See notes below (in bold) to help you figure out where to focus your
efforts.
David J.
From: pgsql-jdbc-owner@postgresql.org
[mailto:pgsql-jdbc-owner@postgresql.org] On Behalf Of
Benjamin.Galaviz@LSGSkyChefs.com
Sent: Wednesday, June 08, 2011 1:37 PM
To: pgsql-jdbc@postgresql.org
Subject: [JDBC] PGPoolingDataSource problem.
private static Connection conn = null;
public Connection getConnection(){ ---- You are returning a static
variable from a non-static method...
PGPoolingDataSource pgdb = (PGPoolingDataSource)getInstance();
conn = null; ---- and what if there was already another
connection present..
try{
conn = pgdb.getConnection();
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public static void closeConnection(){
if (conn != null){
try{conn.close();conn = null;}catch(Exception e){} ----
NEVER IGNORE EXCEPTIONS; ESPECIALLY CATCH (Exception e)
}
}
try{DatabasePool.closeConnection();db=null;}catch(Exception e){}
---- NEVER IGNORE EXCEPTIONS; ESPECIALLY CATCH (Exception e)
!!!!!! You are likely capturing - then ignoring - a NullPointerException
here !!!!!!!!!
Private String getValue(String XX){
PreparedStatement ps =
db.getConnection().preparedStatement(sql);
ResultSet rs = ps.executeQuery();
While (rs.next()){
retVal = rs.getString(1);
}
try{DatabasePool.closeConnection();db=null;}catch(Exception
e){}
}
}
--- Open and close within the same function is OK IF there was no connection
already open
String myValue = getValue(XX); ----Open
and Close
PreparedStatement ps =
db.getConnection().preparedStatement(sql); --- Open
try{DatabasePool.closeConnection();db=null;}catch(Exception
e){} ----Close
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: polobo@yahoo.com, Benjamin.Galaviz@LSGSkyChefs.com
Subject: Re: PGPoolingDataSource problem.
In-Reply-To: <02f801cc260a$7c0df640$7429e2c0$@yahoo.com>
References: <E2435754E1952542B9E77BDDC3E83942076D9EA7@sw-adclsg-mbx01.americas.lsgsc.com> <02f801cc260a$7c0df640$7429e2c0$@yahoo.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox