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
This commit is contained in:
Richard G. Curtis 2012-11-05 19:55:44 +00:00
parent 2980c1901e
commit 60b97e307f
3 changed files with 61 additions and 15 deletions

View File

@ -27,11 +27,9 @@ import java.util.ArrayList;
import java.util.BitSet; import java.util.BitSet;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -110,7 +108,11 @@ public class JDBCStoreManager implements StoreManager, JDBCStore {
// track the pending statements so we can cancel them // track the pending statements so we can cancel them
private List<Statement> _stmnts = Collections.synchronizedList(new ArrayList<Statement>()); 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() { public StoreContext getContext() {
return _ctx; return _ctx;
} }
@ -1630,30 +1632,53 @@ public class JDBCStoreManager implements StoreManager, JDBCStore {
} }
protected Statement createStatement(boolean wrap) throws SQLException { protected Statement createStatement(boolean wrap) throws SQLException {
return new CancelStatement(super.createStatement(false), return getCancelStatement(super.createStatement(false),
RefCountConnection.this); RefCountConnection.this);
} }
protected Statement createStatement(int rsType, int rsConcur, protected Statement createStatement(int rsType, int rsConcur,
boolean wrap) throws SQLException { boolean wrap) throws SQLException {
return new CancelStatement(super.createStatement(rsType, rsConcur, return getCancelStatement(super.createStatement(rsType, rsConcur,
false), RefCountConnection.this); false), RefCountConnection.this);
} }
protected PreparedStatement prepareStatement(String sql, boolean wrap) protected PreparedStatement prepareStatement(String sql, boolean wrap)
throws SQLException { throws SQLException {
return new CancelPreparedStatement(super.prepareStatement(sql, return getCancelPreparedStatement(super.prepareStatement(sql,
false), RefCountConnection.this); false), RefCountConnection.this);
} }
protected PreparedStatement prepareStatement(String sql, int rsType, protected PreparedStatement prepareStatement(String sql, int rsType,
int rsConcur, boolean wrap) throws SQLException { int rsConcur, boolean wrap) throws SQLException {
return new CancelPreparedStatement(super.prepareStatement(sql, return getCancelPreparedStatement(super.prepareStatement(sql,
rsType, rsConcur, false), RefCountConnection.this); 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 * Statement type that adds and removes itself from the set of active
* statements so that it can be canceled. * statements so that it can be canceled.
@ -1745,6 +1770,13 @@ public class JDBCStoreManager implements StoreManager, JDBCStore {
afterExecuteStatement(this); 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); afterExecuteStatement(this);
} }
} }
public void close() throws SQLException {
super.close();
synchronized (_cancelPreparedStatementsPool) {
_cancelPreparedStatementsPool.add(this);
}
}
} }
} }

View File

@ -54,12 +54,15 @@ import org.apache.openjpa.lib.util.Closeable;
public class DelegatingPreparedStatement public class DelegatingPreparedStatement
implements PreparedStatement, Closeable { implements PreparedStatement, Closeable {
private final PreparedStatement _stmnt; private /*final*/ PreparedStatement _stmnt;
private final DelegatingPreparedStatement _del; private /*final*/ DelegatingPreparedStatement _del;
private final Connection _conn; private /*final*/ Connection _conn;
public DelegatingPreparedStatement(PreparedStatement stmnt, public DelegatingPreparedStatement(PreparedStatement stmnt, Connection conn) {
Connection conn) { initialize(stmnt, conn);
}
public void initialize(PreparedStatement stmnt, Connection conn) {
_conn = conn; _conn = conn;
_stmnt = stmnt; _stmnt = stmnt;
if (_stmnt instanceof DelegatingPreparedStatement) if (_stmnt instanceof DelegatingPreparedStatement)

View File

@ -36,11 +36,15 @@ import org.apache.openjpa.lib.util.Closeable;
*/ */
public class DelegatingStatement implements Statement, Closeable { public class DelegatingStatement implements Statement, Closeable {
private final Statement _stmnt; private Statement _stmnt;
private final DelegatingStatement _del; private DelegatingStatement _del;
private final Connection _conn; private Connection _conn;
public DelegatingStatement(Statement stmnt, Connection conn) { public DelegatingStatement(Statement stmnt, Connection conn) {
initialize(stmnt, conn);
}
public void initialize(Statement stmnt, Connection conn) {
_conn = conn; _conn = conn;
_stmnt = stmnt; _stmnt = stmnt;
if (stmnt instanceof DelegatingStatement) if (stmnt instanceof DelegatingStatement)