pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Chris Wareham <cwareham@visitlondon.com>
To: Benjamin.Galaviz@LSGSkyChefs.com
Cc: pgsql-jdbc@postgresql.org
Subject: Re: PGPoolingDataSource problem.
Date: Thu, 9 Jun 2011 11:33:05 +0100
Message-ID: <4DF0A161.8030203@visitlondon.com> (raw)
In-Reply-To: <E2435754E1952542B9E77BDDC3E83942076D9EA7@sw-adclsg-mbx01.americas.lsgsc.com>
References: <E2435754E1952542B9E77BDDC3E83942076D9EA7@sw-adclsg-mbx01.americas.lsgsc.com>

On 06/08/11 18:36, Benjamin.Galaviz@LSGSkyChefs.com wrote:
> I am using the JDBC3 PGPoolingDataSource to create a pool of 5 connections
> (for test) to my database.
>
> public class DatabasePool {
>

Improved and corrected (albeit untested) versions of your code below -
but please read my comment at the end of this message on why you don't
want to reinvent the wheel.

public class ConnectionFactory {
     private static PGPoolingDataSource ds;

     private ConnectionFactory() {
         // enforce singleton
     }

     private static synchronized DataSource getDataSource() throws
SQLException {
         if (ds == null){
             ds = new PGPoolingDataSource();
             ds.setDataSourceName("DataSource");
             ds.setServerName("XX.XX.XX.XX");
             ds.setDatabaseName("dbname");
             ds.setUser("user");
             ds.setPassword("password");
             ds.setMaxConnections(5);
         }

         return ds;
     }

     public static Connection getConnection() throws SQLException {
         return getDataSource.getConnection();
     }

     public static void releaseConnection(final Connection connection) {
         if (connection != null) {
             try {
                 connection.close();
             } catch (SQLException exception) {
                 // log warning ...
             }
         }
     }
}
>
> I get a leaked connections when I grab more than one connection from the
> pool at a time.
>
> Public void insertData(){
>
> Private String getValue(String XX){
>

public void insertData(final String sql) throws SQLException {
     Connection connection = null;

     try {
         connection = ConnectionFactory.getConnection();

         PreparedStatement ps = connection.preparedStatement(sql);
         ps.setString(1, "foo");
         ps.executeUpdate();
         ps.close();
     } finally{
         ConnectionFactory.releaseConnection(connection);
     }
}

public String getValue(final String sql, final String foo) throws
SQLException {
     String value = null;

     Connection connection = null;

     try {
         connection = ConnectionFactory.getConnection();

         PreparedStatement ps = connection.preparedStatement(sql);

         ResultSet rs = ps.executeQuery();

         if (rs.next()) {
             value = rs.getString(1);
         }

         rs.close();

         ps.close();
     } finally{
         ConnectionFactory.releaseConnection(connection);
     }

     return value;
}

However, you'd be much better served by using a framework that hides
the verboseness of JDBC. The Spring framework has an excellent wrapper
for JDBC that also supports pooling, and deals well with the various
niggles that such a wrapper exposes.

Chris



 
Chris Wareham
Senior Software Engineer
London & Partners
6th floor,
2 More London Riverside
London SE1 2RR
 
Tel: +44 (0)20 7234 5848
Fax: +44 (0)20 7234 5753
 
http://www.londonandpartners.com/
 
 
'London & Partners Limited' is registered in England under No.7493460;
Registered Office:London & Partners, City Hall, The Queen's Walk, London SE1 2AA.
 
London & Partners is the official promotional agency for London attracting and delivering value to businesses, students and visitors. London & Partners is a not-for-profit public private partnership, funded by the Mayor of London and our network of commercial partners.
 
The information contained in this e-mail is confidential and intended for the named recipient(s) only. If you have received it in error, please notify the sender immediately and then delete the message. If you are not the intended recipient, you must not use, disclose, copy or distribute this email. The views expressed in this e-mail are those of the individual and not of London & Partners Limited. We reserve the right to read and monitor any email or attachment entering or leaving our systems without prior notice.
 
 Please don't print this e-mail unless you really need to.



view thread (8+ messages)  latest in thread

Message-ID: <4DF0A161.8030203@visitlondon.com>
Permalink:  ../4DF0A161.8030203@visitlondon.com/
Also on:    postgresql.org/message-id/4DF0A161.8030203@visitlondon.com

 · 

reply

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: cwareham@visitlondon.com, Benjamin.Galaviz@LSGSkyChefs.com
  Subject: Re: PGPoolingDataSource problem.
  In-Reply-To: <4DF0A161.8030203@visitlondon.com>
  References: <E2435754E1952542B9E77BDDC3E83942076D9EA7@sw-adclsg-mbx01.americas.lsgsc.com> <4DF0A161.8030203@visitlondon.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