openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From curti...@apache.org
Subject svn commit: r1405936 - in /openjpa/branches/2.2.1.x: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/ openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/
Date Mon, 05 Nov 2012 20:23:30 GMT
Author: curtisr7
Date: Mon Nov  5 20:23:29 2012
New Revision: 1405936

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

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

Modified: openjpa/branches/2.2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java?rev=1405936&r1=1405935&r2=1405936&view=diff
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
(original)
+++ openjpa/branches/2.2.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
Mon Nov  5 20:23:29 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/branches/2.2.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingPreparedStatement.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingPreparedStatement.java?rev=1405936&r1=1405935&r2=1405936&view=diff
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingPreparedStatement.java
(original)
+++ openjpa/branches/2.2.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingPreparedStatement.java
Mon Nov  5 20:23:29 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/branches/2.2.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingStatement.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingStatement.java?rev=1405936&r1=1405935&r2=1405936&view=diff
==============================================================================
--- openjpa/branches/2.2.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingStatement.java
(original)
+++ openjpa/branches/2.2.1.x/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingStatement.java
Mon Nov  5 20:23:29 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)



Mime
View raw message