Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 4CE19B5DD41 for ; Thu, 9 Jun 2011 04:31:02 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 29798-03 for ; Thu, 9 Jun 2011 07:30:55 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.softperience.eu (static-ip-217-118-26-141.inaddr.intergenia.de [217.118.26.141]) by mail.postgresql.org (Postfix) with ESMTP id ED02AB5DD43 for ; Thu, 9 Jun 2011 04:30:54 -0300 (ADT) Received: from mail.softperience.eu (static-ip-217-118-26-141.inaddr.intergenia.de [217.118.26.141]) by mail.softperience.eu (Postfix) with ESMTPA id 70076C000C2; Thu, 9 Jun 2011 09:30:41 +0200 (CEST) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Date: Thu, 09 Jun 2011 09:30:41 +0200 From: =?UTF-8?Q?Rados=C5=82aw_Smogura?= To: Craig Ringer Cc: , Subject: Re: PGPoolingDataSource problem. In-Reply-To: <4DF04704.8070604@postnewspapers.com.au> References: <4DF04704.8070604@postnewspapers.com.au> Message-ID: X-Sender: rsmogura@softperience.eu User-Agent: Roundcube Webmail/0.5.1 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 X-Spam-Level: X-Archive-Number: 201106/18 X-Sequence-Number: 21942 On Thu, 09 Jun 2011 12:07:32 +0800, Craig Ringer wrote: > On 09/06/11 01:36, Benjamin.Galaviz@LSGSkyChefs.com wrote: >> I am using the JDBC3 PGPoolingDataSource to create a pool of 5 >> connections (for test) to my database. > > I'd recommend using a proper connection pool like C3P0 or DBCP. It'll > be > less work in the long run, as PGPoolingDataSource is more of a > toy/testing tool. > > What's your environment, anyway? Are you running in a standalone JVM > (j2SE) or in an application server context? If you're in an appserver > or > servlet container you should be getting connections from the > container > environment. > > -- > Craig Ringer From other posts, it looks like C3P0 is only JDBC2 aware. so you will automatic mange them. Actually PoolingDataSource could track leak and add auto-close, I was sure it could do this, but I'm wrong. Backing to original post... From design point of view your implementation in not quite good, bad not bad. - You use singleton pattern to manage DataSource and it's good. - But private static Connection conn = null; should it really be static? getConnection is not static this causes leak. - And one tip - when I make singleton pattern I, mainly, create for class keeping this value private default constructor, so no one who use my code will not accidental call new Singleton, instead of Singleton.getInstance(). - Just fix pool, and if you have time wrap your connections managed by pool in WeakReference (eventually adding pool to this reference). This is how pools are implemented. Everyone know to close connection but smart developers know that many forget it, and adding leaking support may save your time in future on writing try / catch trees. Have a luck. Regards, Radek