Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 308D9632D3F for ; Thu, 12 Mar 2009 07:14:34 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 73358-07 for ; Thu, 12 Mar 2009 07:14:24 -0300 (ADT) X-Greylist: delayed 00:09:08.352105 by SQLgrey-1.7.6 Received: from mail2.fintrading.com (mail.fintrading.com [195.2.100.100]) by mail.postgresql.org (Postfix) with ESMTP id E6576633D4D for ; Thu, 12 Mar 2009 07:14:28 -0300 (ADT) Received: from chipmunk (unknown [81.198.70.254]) by mail2.fintrading.com (Postfix) with ESMTPA id D5E32381138B; Thu, 12 Mar 2009 12:04:28 +0200 (EET) From: "Peter" To: "'Kris Jurka'" Cc: References: <49b7bfef$0$1343$834e42db@reader.greatnowhere.com> <49B7CBB6.5020800@opencloud.com> <49b7ced9$0$1343$834e42db@reader.greatnowhere.com> In-Reply-To: Subject: Re: Totally weird behaviour in org.postgresql.Driver Date: Thu, 12 Mar 2009 12:04:27 +0200 Message-ID: <002201c9a2f9$edd47510$c97d5f30$@com> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: AcmixOU85eQdFn9sQDCOGRz0SHQfoQAM8UeQ Content-Language: en-us X-MailScanner-ID: D5E32381138B.51B9C X-greatnowhere.com-MailScanner: Found to be clean X-greatnowhere.com-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-6.8, required 6, autolearn=not spam, ALL_TRUSTED -1.80, BAYES_00 -5.00) X-greatnowhere.com-MailScanner-From: peter@greatnowhere.com MailScanner-NULL-Check: 1237457072.47125@JD+0pktnNAXCBrCKXjqsTQ X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200903/61 X-Sequence-Number: 19391 >> I added a bunch more synchronized (mutex) blocks in my code to avoid >> getConnection being called simultaneously from multiple threads (why >> isnt that method synchronized anyway?) - so far so good... keeping >> fingers crossed. Maybe should have just subclassed PGSimpleDataSource... >I'm not sure why it needs to be synchronized. The attached test case (20 threads each opening and closing >connections as fast as possible) doesn't show any problems for me. Does it fail for you? Is there a >particular change that fixes it for you? I'm not sure if it needs to be synchronized either. Most likely I'm running into a JVM bug in my particular environment (Tomcat servlet). I adapted your test case to run inside a servlet and it does not produce any errors. In my app I'm using singleton class instance to handle all database connections, sample code below. If I remove synchronized (dbManager.class) blocks the bug is back with a vengeance... final public class dbManager { private static PGSimpleDataSource _ds; // Our global instance private static dbManager _dbManager = null; /** * Attempts to create a valid PG DataSource * * @return a boolean representing whether the DS was successfully created */ private synchronized static boolean createDataSource() { try { synchronized (dbManager.class) { _ds = new PGSimpleDataSource(); _ds.setServerName("myserver"); _ds.setDatabaseName("mydb"); _ds.setPortNumber(5432); } return true; } catch (Exception ex) { Logger.getLogger(dbManager.class.getName()).log(Level.SEVERE, null, ex); return false; } } public synchronized static dbManager getInstance(String connString) { if ( _dbManager == null ) { if ( createDataSource() ) { _dbManager = new dbManager(); } } return _dbManager; } /** * * @return returns a connection created w/ default initial credentials and very little access rights */ public synchronized Connection getInitialConnection() throws SQLException { synchronized (dbManager.class) { return _ds.getConnection("inituser", "initpass"); } } /** * * @param dbUser * @param dbPass * @return a connection if it can be created, null otherwise */ public synchronized Connection getNewUserConnection(String dbUser, String dbPass){ try { synchronized (dbManager.class) { Connection userConn = _ds.getConnection(dbUser, dbPass); return userConn; } } catch (Exception ex) { Logger.getLogger(dbManager.class.getName()).log(Level.SEVERE, null, ex); return null; } } }