PGPoolingDataSource problem. - Mailing list pgsql-jdbc
From | |
---|---|
Subject | PGPoolingDataSource problem. |
Date | |
Msg-id | E2435754E1952542B9E77BDDC3E83942076D9EA7@sw-adclsg-mbx01.americas.lsgsc.com Whole thread Raw |
Responses |
Re: PGPoolingDataSource problem.
Re: PGPoolingDataSource problem. Re: PGPoolingDataSource problem. |
List | 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){}
}
}
Attachment
pgsql-jdbc by date: