jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From un...@apache.org
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 GMT
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<String, Connection> batchConnectionMap = Collections.synchronizedMap(new
HashMap<String, Connection>());
+    private Map<Object, Connection> batchConnectionMap = Collections.synchronizedMap(new
HashMap<Object, Connection>());
 
     /**
      * 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);
+        }
+    }
 }



Mime
View raw message