Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 3031E634802 for ; Sat, 12 Dec 2009 21:37:59 -0400 (AST) 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 52220-06 for ; Sun, 13 Dec 2009 01:37:40 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mta1.zcs.ext.wpsrv.net (mta1.zcs.ext.wpsrv.net [78.47.55.244]) by mail.postgresql.org (Postfix) with ESMTP id 870C56347EF for ; Sat, 12 Dec 2009 21:37:48 -0400 (AST) Received: from localhost (localhost [127.0.0.1]) by mta1.zcs.ext.wpsrv.net (Postfix) with ESMTP id E042E18052 for ; Sun, 13 Dec 2009 02:37:45 +0100 (CET) X-Virus-Scanned: amavisd-new at mta1.zcs.ext.wpsrv.net Received: from mta1.zcs.ext.wpsrv.net ([127.0.0.1]) by localhost (mta1.zcs.ext.wpsrv.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id LG3gZkbGc07Z for ; Sun, 13 Dec 2009 02:37:45 +0100 (CET) Received: from store1.zcs.ext.wpsrv.net (store1.zcs.ext.wpsrv.net [78.47.55.243]) by mta1.zcs.ext.wpsrv.net (Postfix) with ESMTP id 1EE6E1803F for ; Sun, 13 Dec 2009 02:37:45 +0100 (CET) Date: Sun, 13 Dec 2009 02:37:44 +0100 (CET) From: Andreas To: pgsql-jdbc@postgresql.org Message-ID: <21846658.35.1260668264808.JavaMail.root@store1.zcs.ext.wpsrv.net> In-Reply-To: <98B3D2B2-9F95-43CA-AA0A-02CD8ACE771B@groupspeak.com> Subject: Re: keeping Connection alive MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Mailer: Zimbra 6.0.2_GA_1912.DEBIAN4.0 (ZimbraWebClient - FF3.0 (Linux)/6.0.2_GA_1912.DEBIAN4.0) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/34 X-Sequence-Number: 20115 Hi Jovan, thanks for your reply! > On Dec 12, 2009, at 6:43 PM, Andreas Brandl wrote: > > I'm using a PostgreSQL as backend for a Java application, which may > idle for quite a while (i.e. hours) and now and then issue a query on > the backend. > > Is this a standard Java application or is it running in a Java EE or > servlet container? This is a standard Java application. > If you're not using an application server, and/or don't need pooling, > perhaps it would be better to use the regular jdbc api something like > this: > Class.forName("org.postgresql.Driver"); > Connection conn = DriverManager.getConnection(jdbcConnUrl, username, > password); > // then conn.createStatement , etc. > > Then you can call conn.close() and create a new connection when > necessary. I did work with a pure Connection before, until I realized things go wrong when application is idle for long time. In either case, the problem is how to detect whether a Connection is valid or not. As far as I know there is no native mechanism for checking the state. So one approach may be to implement a DataSource which does check (i.e. "SELECT 1") validity before returning the Connection (and reconnect if its not valid, i.e. throwing a SQLException?). Is there any better way to achieve this? After all this might mean a lot of overhead (checking every time DataSource.getConnection() is called)... Thanks, Andreas