Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 65043B5DBE7 for ; Thu, 9 Jun 2011 08:49:13 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 42020-06 for ; Thu, 9 Jun 2011 11:49:05 +0000 (UTC) X-Greylist: delayed 01:15:51.616976 by SQLgrey-1.7.6 Received: from claranet-outbound-smtp05.uk.clara.net (claranet-outbound-smtp05.uk.clara.net [195.8.89.38]) by mail.postgresql.org (Postfix) with ESMTP id 2DFF4B5DBE6 for ; Thu, 9 Jun 2011 08:49:04 -0300 (ADT) Received: from [217.158.205.40] (port=63005 helo=BSP1.visitlondon.com) by relay05.mail.eu.clara.net (relay.clara.net [213.253.3.45]:1025) with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) id 1QUcY0-0008N6-If (return-path ); Thu, 09 Jun 2011 10:33:08 +0000 thread-index: AcwmkJ6OYb2C8eiQTyCfONjQ52aPAg== Content-Class: urn:content-classes:message Importance: normal Message-ID: <4DF0A161.8030203@visitlondon.com> X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6002.18417 Date: Thu, 9 Jun 2011 11:33:05 +0100 From: Chris Wareham Organization: Visit London User-Agent: Mozilla/5.0 (X11; U; NetBSD i386; en-US; rv:1.9.2.17) Gecko/20110505 Lightning/1.0b3pre Thunderbird/3.1.10 MIME-Version: 1.0 To: CC: Subject: Re: PGPoolingDataSource problem. References: In-Reply-To: Content-Type: text/plain; format=flowed; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Received-SPF: none X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201106/21 X-Sequence-Number: 21945 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 =3D=3D null){ ds =3D 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 !=3D 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 =3D null; try { connection =3D ConnectionFactory.getConnection(); PreparedStatement ps =3D 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 =3D null; Connection connection =3D null; try { connection =3D ConnectionFactory.getConnection(); PreparedStatement ps =3D connection.preparedStatement(sql); ResultSet rs =3D ps.executeQuery(); if (rs.next()) { value =3D 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 =20 Chris Wareham Senior Software Engineer London & Partners 6th floor, 2 More London Riverside London SE1 2RR =20 Tel: +44 (0)20 7234 5848 Fax: +44 (0)20 7234 5753 =20 http://www.londonandpartners.com/ =20 =20 'London & Partners Limited' is registered in England under No.7493460; Registered Office:London & Partners, City Hall, The Queen's Walk, London = SE1 2AA. =20 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. =20 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. =20 Please don't print this e-mail unless you really need to.