Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id AB03CD84656 for ; Fri, 8 Jun 2012 10:43:57 -0300 (ADT) Received: from mail-pb0-f46.google.com ([209.85.160.46]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SczTm-0003Qc-FN for pgsql-jdbc@postgresql.org; Fri, 08 Jun 2012 13:43:56 +0000 Received: by pbbrp8 with SMTP id rp8so2566161pbb.19 for ; Fri, 08 Jun 2012 06:43:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=Z4p2bYLv5/4Njbb1aVwFIYSBPhy9m+dqEnSrDCfrSO4=; b=H5rytJI8+Vc9jkdn89xbSieTfdFFu9L3xWaA0YucuUR3B2wnBMdfHiTgA5kIxTMTXP fj0bsl1mYC5i3GdQ0xWby7CiAMeQXtEvb5zL/hViTlYMyQrQwaAi+UBx9EHjX6+gXKl1 WV3LYF+oT9J8AzmiMk1a6ylPhSviG86T5DgpdrisN9gnPjkPr5Tud9XBN0/WVJ4Z6gAT ZDqYLQtI/zrhUvkc79JE1h1mYKKRUhcwHcpzfl8tw0CQmZXrl9pEGHLppAOUHblpXrwS Uunr/I0kbtNxpoJTgAthpnc6W7udb/Lq1rTUec3Mguuki+qYAG/ZAjkttVnKerfgdBgy lclw== Received: by 10.68.229.2 with SMTP id sm2mr20097694pbc.57.1339163020113; Fri, 08 Jun 2012 06:43:40 -0700 (PDT) Received: from ayaki.localdomain (124-169-197-239.dyn.iinet.net.au. [124.169.197.239]) by mx.google.com with ESMTPS id jw3sm7878188pbc.65.2012.06.08.06.43.37 (version=SSLv3 cipher=OTHER); Fri, 08 Jun 2012 06:43:39 -0700 (PDT) Message-ID: <4FD20186.8050807@ringerc.id.au> Date: Fri, 08 Jun 2012 21:43:34 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120430 Thunderbird/12.0.1 MIME-Version: 1.0 To: Vlad Arkhipov CC: pgsql-jdbc@postgresql.org Subject: Re: Connection.isValid(int timeout) implementation References: <4FD1E2BE.8020800@dc.baikal.ru> In-Reply-To: <4FD1E2BE.8020800@dc.baikal.ru> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQnNyuRTLYP5oxn88dCk6WlJ+QFusTThXMi8EpLZ5JBF139Tpa2hPDta8ly7w1YecWUtRrpk X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201206/28 X-Sequence-Number: 23022 On 06/08/2012 07:32 PM, Vlad Arkhipov wrote: > The current PostgreSQL driver (9.1-904) does not contain an > implementation of JDBC4 method Connection.isValid(int timeout). Looks like it's since been implemented. It was commited by Dave Cramer on 2012-01-19 in commit 773f8e45, based on a patch by Louis Flores. commit 773f8e45fada12357d7b8650278fa3d37ac4d237 Author: Dave Cramer Date: Thu Jan 19 20:00:51 2012 +0000 added isValid implementation from Luis Flores $ git blame ./org/postgresql/jdbc4/AbstractJdbc4Connection.java 0cf874a4 (Kris Jurka 2006-06-08 10:34:52 +0000 116) public boolean isValid(int timeout) throws SQLException 0cf874a4 (Kris Jurka 2006-06-08 10:34:52 +0000 117) { 3f525f1a (Kris Jurka 2009-11-18 11:19:31 +0000 118) checkClosed(); 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 119) if (timeout < 0) { 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 120) throw new PSQLException(GT.tr("Invalid timeout ({0}<0).", timeout), PSQLState.INVALID_PARAMETER_VALUE); 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 121) } 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 122) boolean valid = false; 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 123) Statement stmt = null; 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 124) try { 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 125) if (!isClosed()) { 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 126) stmt = createStatement(); 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 127) stmt.setQueryTimeout( timeout ); 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 128) stmt.executeQuery( "SELECT 1" ); 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 129) valid = true; 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 130) } 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 131) } 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 132) catch ( SQLException e) { 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 133) getLogger().log(GT.tr("Validating connection."),e); 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 134) } 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 135) finally 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 136) { 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 137) if(stmt!=null) try {stmt.close();}catch(Exception ex){} 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 138) } 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 139) return valid; 773f8e45 (Dave Cramer 2012-01-19 20:00:51 +0000 140) } > However > there are some projects that strongly rely on this method to test the > connection (for example Arjuna Transaction Manager that is used by JBoss > Application Server). Are there any plans of implementing this feature? Really? JBoss AS 7.1.1.Final is quite happy with PgJDBC (postgresql-9.1-901-1.jdbc4.jar) here. What's your configuration? Your -ds.xml or jboss-cli "create datasource" command? IMO the whole "validate" concept in JDBC is pretty broken, but that doesn't mean PgJDBC shouldn't support it. The only way to validate a connection is to use it. If it doesn't work, close it and get a new one. Any other approach courts race conditions between isValid() and the use of the connection. It's arguably still OK to use isValid() to proactively weed dead connections after server restarts to avoid the case where an app gets a replacement for a dead connection and it's dead too, as it its replacement, and so on. It's often misused, though. BTW, I'm not convinced we should be sending a "SELECT 1;". It's valid to send an empty string and I'd be inclined to do that instead. Still, I presume there's a reason `SELECT 1` was used. -- Craig Ringer