From 60b97e307fa4b454082dc6a715638d9899133af0 Mon Sep 17 00:00:00 2001 From: "Richard G. Curtis" Date: Mon, 5 Nov 2012 19:55:44 +0000 Subject: [PATCH] OPENJPA-2292: Pool CancelPreparedStatements to reduce object allocations. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1405929 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/jdbc/kernel/JDBCStoreManager.java | 53 ++++++++++++++++--- .../lib/jdbc/DelegatingPreparedStatement.java | 13 +++-- .../openjpa/lib/jdbc/DelegatingStatement.java | 10 ++-- 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java index 65abde2fb..f25bb3338 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java @@ -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 StoreManager, JDBCStore { // track the pending statements so we can cancel them private List _stmnts = Collections.synchronizedList(new ArrayList()); - + + // pool statements so that we can try to reuse rather than recreate + private List _cancelPreparedStatementsPool = new ArrayList(); + private List _cancelStatementPool = new ArrayList(); + public StoreContext getContext() { return _ctx; } @@ -1630,30 +1632,53 @@ public class JDBCStoreManager implements StoreManager, JDBCStore { } 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 StoreManager, JDBCStore { afterExecuteStatement(this); } } + + public void close() throws SQLException { + super.close(); + synchronized (_cancelStatementPool) { + _cancelStatementPool.add(this); + } + } } /** @@ -1866,6 +1898,13 @@ public class JDBCStoreManager implements StoreManager, JDBCStore { afterExecuteStatement(this); } } + + public void close() throws SQLException { + super.close(); + synchronized (_cancelPreparedStatementsPool) { + _cancelPreparedStatementsPool.add(this); + } + } } } diff --git a/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingPreparedStatement.java b/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingPreparedStatement.java index bc54cc2dd..a9b39db91 100644 --- a/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingPreparedStatement.java +++ b/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingPreparedStatement.java @@ -54,12 +54,15 @@ import org.apache.openjpa.lib.util.Closeable; 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) diff --git a/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingStatement.java b/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingStatement.java index d4ce9284d..d2dc014e6 100644 --- a/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingStatement.java +++ b/openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/DelegatingStatement.java @@ -36,11 +36,15 @@ import org.apache.openjpa.lib.util.Closeable; */ 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)