Return-Path: X-Original-To: apmail-jackrabbit-commits-archive@www.apache.org Delivered-To: apmail-jackrabbit-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2752FD53C for ; Thu, 27 Sep 2012 09:55:24 +0000 (UTC) Received: (qmail 27548 invoked by uid 500); 27 Sep 2012 09:55:24 -0000 Delivered-To: apmail-jackrabbit-commits-archive@jackrabbit.apache.org Received: (qmail 27479 invoked by uid 500); 27 Sep 2012 09:55:23 -0000 Mailing-List: contact commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jackrabbit.apache.org Delivered-To: mailing list commits@jackrabbit.apache.org Received: (qmail 27464 invoked by uid 99); 27 Sep 2012 09:55:23 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 27 Sep 2012 09:55:23 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 27 Sep 2012 09:55:22 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 13B6323888CD; Thu, 27 Sep 2012 09:54:39 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1390912 - /jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/db/ConnectionHelper.java Date: Thu, 27 Sep 2012 09:54:38 -0000 To: commits@jackrabbit.apache.org From: unico@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120927095439.13B6323888CD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: unico Date: Thu Sep 27 09:54:38 2012 New Revision: 1390912 URL: http://svn.apache.org/viewvc?rev=1390912&view=rev Log: JCR-3387 Backport: On heavy load we see occasional SQLException: closed statement: next Modified: jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/db/ConnectionHelper.java Modified: jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/db/ConnectionHelper.java URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/db/ConnectionHelper.java?rev=1390912&r1=1390911&r2=1390912&view=diff ============================================================================== --- jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/db/ConnectionHelper.java (original) +++ jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/db/ConnectionHelper.java Thu Sep 27 09:54:38 2012 @@ -22,6 +22,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -80,7 +81,7 @@ public class ConnectionHelper { protected final DataSource dataSource; - private Map batchConnectionMap = Collections.synchronizedMap(new HashMap()); + private Map batchConnectionMap = Collections.synchronizedMap(new HashMap()); /** * The default fetchSize is '0'. This means the fetchSize Hint will be ignored @@ -449,7 +450,7 @@ public class ConnectionHelper { */ private Connection getTransactionAwareBatchConnection() { Object threadId = TransactionContext.getCurrentThreadId(); - return batchConnectionMap.get(threadIdToString(threadId)); + return batchConnectionMap.get(threadIdToObject(threadId)); } /** @@ -461,7 +462,7 @@ public class ConnectionHelper { */ private void setTransactionAwareBatchConnection(Connection batchConnection) { Object threadId = TransactionContext.getCurrentThreadId(); - batchConnectionMap.put(threadIdToString(threadId), batchConnection); + batchConnectionMap.put(threadIdToObject(threadId), batchConnection); } /** @@ -469,7 +470,7 @@ public class ConnectionHelper { */ private void removeTransactionAwareBatchConnection() { Object threadId = TransactionContext.getCurrentThreadId(); - batchConnectionMap.remove(threadIdToString(threadId)); + batchConnectionMap.remove(threadIdToObject(threadId)); } /** @@ -478,22 +479,9 @@ public class ConnectionHelper { * @param threadId * @return String */ - private String threadIdToString(Object threadId) { + private Object threadIdToObject(Object threadId) { if (threadId instanceof byte[]) { - byte[] gtrid = (byte[]) threadId; - int hexVal; - StringBuffer sb = new StringBuffer(512); - sb.append(" gtrid(" + gtrid.length + ")={0x"); - for (int i=0; i< gtrid.length; i++) { - hexVal = gtrid[i]&0xFF; - if ( hexVal < 0x10 ) { - sb.append("0" + Integer.toHexString(gtrid[i]&0xFF)); - } else { - sb.append(Integer.toHexString(gtrid[i]&0xFF)); - } - } - sb.append("}"); - return sb.toString(); + return new XidWrapper((byte[]) threadId); } else { return threadId.toString(); } @@ -579,4 +567,29 @@ public class ConnectionHelper { protected abstract T call() throws SQLException; } + + /** + * Wrapper around a global transaction id (byte[]) + * that handles hashCode and equals in a proper way. + */ + private class XidWrapper { + private byte[] gtid; + + public XidWrapper(byte[] gtid) { + this.gtid = gtid; + } + + @Override + public boolean equals(Object other) { + if (!(other instanceof XidWrapper)) { + return false; + } + return TransactionContext.isSameThreadId(gtid, ((XidWrapper)other).gtid); + } + + @Override + public int hashCode() { + return Arrays.hashCode(gtid); + } + } }