You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by cu...@apache.org on 2012/11/05 20:55:44 UTC

svn commit: r1405929 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/ openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/

Author: curtisr7
Date: Mon Nov  5 19:55:44 2012
New Revision: 1405929

URL: http://svn.apache.org/viewvc?rev=1405929&view=rev
Log:
OPENJPA-2292: Pool CancelPreparedStatements to reduce object allocations.

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
    openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingPreparedStatement.java
    openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingStatement.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java?rev=1405929&r1=1405928&r2=1405929&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java Mon Nov  5 19:55:44 2012
@@ -27,11 +27,9 @@ import java.util.ArrayList;
 import java.util.BitSet;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 import javax.sql.DataSource;
 
@@ -110,7 +108,11 @@ public class JDBCStoreManager implements
     
     // track the pending statements so we can cancel them
     private List<Statement> _stmnts = Collections.synchronizedList(new ArrayList<Statement>());
-
+    
+    // pool statements so that we can try to reuse rather than recreate
+    private List<CancelPreparedStatement> _cancelPreparedStatementsPool = new ArrayList<CancelPreparedStatement>();
+    private List<CancelStatement> _cancelStatementPool = new ArrayList<CancelStatement>();
+    
     public StoreContext getContext() {
         return _ctx;
     }
@@ -1630,30 +1632,53 @@ public class JDBCStoreManager implements
         }
 
         protected Statement createStatement(boolean wrap) throws SQLException {
-            return new CancelStatement(super.createStatement(false),
+            return getCancelStatement(super.createStatement(false),
                 RefCountConnection.this);
         }
 
         protected Statement createStatement(int rsType, int rsConcur,
             boolean wrap) throws SQLException {
-            return new CancelStatement(super.createStatement(rsType, rsConcur,
+            return getCancelStatement(super.createStatement(rsType, rsConcur,
                 false), RefCountConnection.this);
 
         }
 
         protected PreparedStatement prepareStatement(String sql, boolean wrap)
             throws SQLException {
-            return new CancelPreparedStatement(super.prepareStatement(sql,
+            return getCancelPreparedStatement(super.prepareStatement(sql,
                 false), RefCountConnection.this);
         }
 
         protected PreparedStatement prepareStatement(String sql, int rsType,
             int rsConcur, boolean wrap) throws SQLException {
-            return new CancelPreparedStatement(super.prepareStatement(sql,
+            return getCancelPreparedStatement(super.prepareStatement(sql,
                 rsType, rsConcur, false), RefCountConnection.this);
         }
     }
 
+    private PreparedStatement getCancelPreparedStatement(PreparedStatement stmnt, Connection conn) {
+        synchronized (_cancelPreparedStatementsPool) {
+            if (!_cancelPreparedStatementsPool.isEmpty()) {
+                CancelPreparedStatement res = _cancelPreparedStatementsPool.remove(0);
+                res.initialize(stmnt, conn);
+                return res;
+            }
+        }
+        return new CancelPreparedStatement(stmnt, conn);
+    }
+    
+    private Statement getCancelStatement(Statement stmnt, Connection conn) {
+        synchronized (_cancelStatementPool) {
+            if (!_cancelStatementPool.isEmpty()) {
+                CancelStatement res = _cancelStatementPool.remove(0);
+                res.initialize(stmnt, conn);
+                return res;
+            }
+        }
+
+        return new CancelStatement(stmnt, conn);
+    }
+    
     /**
      * Statement type that adds and removes itself from the set of active
      * statements so that it can be canceled.
@@ -1745,6 +1770,13 @@ public class JDBCStoreManager implements
                 afterExecuteStatement(this);
             }
         }
+
+        public void close() throws SQLException {
+        	super.close();
+    		synchronized (_cancelStatementPool) {
+    			_cancelStatementPool.add(this);
+    		}
+        }
     }
 
     /**
@@ -1866,6 +1898,13 @@ public class JDBCStoreManager implements
                 afterExecuteStatement(this);
             }
         }
+
+        public void close() throws SQLException {
+        	super.close();
+    		synchronized (_cancelPreparedStatementsPool) {
+    			_cancelPreparedStatementsPool.add(this);
+    		}
+        }
     }
 }
 

Modified: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingPreparedStatement.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingPreparedStatement.java?rev=1405929&r1=1405928&r2=1405929&view=diff
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingPreparedStatement.java (original)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingPreparedStatement.java Mon Nov  5 19:55:44 2012
@@ -54,12 +54,15 @@ import org.apache.openjpa.lib.util.Close
 public class DelegatingPreparedStatement
     implements PreparedStatement, Closeable {
     
-    private final PreparedStatement _stmnt;
-    private final DelegatingPreparedStatement _del;
-    private final Connection _conn;
+    private /*final*/ PreparedStatement _stmnt;
+    private /*final*/ DelegatingPreparedStatement _del;
+    private /*final*/ Connection _conn;
 
-    public DelegatingPreparedStatement(PreparedStatement stmnt,
-        Connection conn) {
+    public DelegatingPreparedStatement(PreparedStatement stmnt, Connection conn) {
+    	initialize(stmnt, conn);
+    }
+    
+    public void initialize(PreparedStatement stmnt, Connection conn) {
         _conn = conn;
         _stmnt = stmnt;
         if (_stmnt instanceof DelegatingPreparedStatement)

Modified: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingStatement.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingStatement.java?rev=1405929&r1=1405928&r2=1405929&view=diff
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingStatement.java (original)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingStatement.java Mon Nov  5 19:55:44 2012
@@ -36,11 +36,15 @@ import org.apache.openjpa.lib.util.Close
  */
 public class DelegatingStatement implements Statement, Closeable {
 
-    private final Statement _stmnt;
-    private final DelegatingStatement _del;
-    private final Connection _conn;
+    private Statement _stmnt;
+    private DelegatingStatement _del;
+    private Connection _conn;
 
     public DelegatingStatement(Statement stmnt, Connection conn) {
+    	initialize(stmnt, conn);
+    }
+    
+    public void initialize(Statement stmnt, Connection conn) {
         _conn = conn;
         _stmnt = stmnt;
         if (stmnt instanceof DelegatingStatement)