From: | <Benjamin(dot)Galaviz(at)LSGSkyChefs(dot)com> |
---|---|
To: | <pgsql-jdbc(at)postgresql(dot)org> |
Subject: | PGPoolingDataSource problem. |
Date: | 2011-06-08 17:36:36 |
Message-ID: | E2435754E1952542B9E77BDDC3E83942076D9EA7@sw-adclsg-mbx01.americas.lsgsc.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-jdbc |
I am using the JDBC3 PGPoolingDataSource to create a pool of 5 connections
(for test) to my database.
public class DatabasePool {
private static PGPoolingDataSource dbp = null;
private static Connection conn = null;
public static PGPoolingDataSource getInstance(){
if (dbp == null){
dbp = new PGPoolingDataSource();
try{
PGPoolingDataSource source = new
PGPoolingDataSource();
source.setDataSourceName("DataSource");
source.setServerName("XX.XX.XX.XX");
source.setDatabaseName("dbname");
source.setUser("user");
source.setPassword("password");
source.setMaxConnections(5);
dbp = source;
}catch(Exception e){
e.printStackTrace();
}
}
return dbp;
}
public Connection getConnection(){
PGPoolingDataSource pgdb = (PGPoolingDataSource)getInstance();
conn = null;
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){}
}
}
}
I get a leaked connections when I grab more than one connection from the
pool at a time.
Public void insertData(){
DatabasePool db = new DatabasePool();
Try{
PreparedStatement ps =
db.getConnection().preparedStatement(sql);
Ps.setString(1, getValue(XX);
Ps.execute();
}catch(Exception e){
e.printStackTrace();
} finally{
try{rs.close();rs=null;}catch(Exception e){}
try{ps.close();ps=null;}catch(Exception e){}
try{DatabasePool.closeConnection();db=null;}catch(Exception e){}
}
}
Private String getValue(String XX){
DatabasePool db = new DatabasePool();
String retVal = "";
Try{
PreparedStatement ps =
db.getConnection().preparedStatement(sql);
ResultSet rs = ps.executeQuery();
While (rs.next()){
retVal = rs.getString(1);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{rs.close();rs=null;}catch(Exception e){}
try{ps.close();ps=null;}catch(Exception e){}
try{DatabasePool.closeConnection();db=null;}catch(Exception e){}
}
}
After running through insertData() once, can see 2 connections on the
database. But if I run through it again, then there are 3 connections when I
expect there to still be only two. It works fine until I hit the
maxConnections and then everything craps out. When I move the getValue(XX)
to before opening the first connection, everything works as I should.
Changed insertData()
Public void insertData(){
DatabasePool db = new DatabasePool();
Try{
String myValue = getValue(XX);
PreparedStatement ps =
db.getConnection().preparedStatement(sql);
Ps.setString(1, myValue);
Ps.execute();
}catch(Exception e){
e.printStackTrace();
} finally{
try{rs.close();rs=null;}catch(Exception e){}
try{ps.close();ps=null;}catch(Exception e){}
try{DatabasePool.closeConnection();db=null;}catch(Exception e){}
}
}
From | Date | Subject | |
---|---|---|---|
Next Message | David Johnston | 2011-06-08 18:32:55 | Re: PGPoolingDataSource problem. |
Previous Message | Michal Politowski | 2011-06-06 08:13:23 | Re: [GENERAL] Mixed up protocol packets in server response? |