Subject | Patch: pooled prepared statements |
---|---|
Author | Ken Richard |
Post date | 2003-05-29T15:05:49Z |
I have heard that pooled prepared statements are part of JDBC3.0 but
have not looked at the spec. Here is a patch that will implement
prepared statement pooling. The patch is against the 1.0 version of the
driver.
Pooling is off by default and enabled by calling
FBWrappingDataSource.setPreparedStatementPooling(true);
?
client-java/src/main/org/firebirdsql/jca/.FBManagedConnectionFactory.jav
a.marks
? client-java/src/main/org/firebirdsql/jdbc/.FBConnection.java.marks
Index: client-java/src/main/org/firebirdsql/jca/FBManagedConnection.java
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jca/FBManagedConn
ection.java,v
retrieving revision 1.19
diff -r1.19 FBManagedConnection.java
29a30
client-java/src/main/org/firebirdsql/jca/FBManagedConnectionFactory.java
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jca/FBManagedConn
ectionFactory.java,v
retrieving revision 1.19
diff -r1.19 FBManagedConnectionFactory.java
139c139,141
<
---
<
<
---
<
---
client-java/src/main/org/firebirdsql/jca/FBPoolingConnectionManager.java
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jca/FBPoolingConn
ectionManager.java,v
retrieving revision 1.2
diff -r1.2 FBPoolingConnectionManager.java
50c50,51
<
---
client-java/src/main/org/firebirdsql/jdbc/FBCallableStatement.java
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jdbc/FBCallableSt
atement.java,v
retrieving revision 1.3
diff -r1.3 FBCallableStatement.java
136c136
<
---
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jdbc/FBConnection
.java,v
retrieving revision 1.19
diff -r1.19 FBConnection.java
79c79
<
---
< PreparedStatement stmt = new FBPreparedStatement(this, sql);
< activeStatements.add(stmt);
---
< CallableStatement stmt = new FBCallableStatement(this, sql);
< activeStatements.add(stmt);
< return stmt;
---
client-java/src/main/org/firebirdsql/jdbc/FBPreparedStatement.java
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jdbc/FBPreparedSt
atement.java,v
retrieving revision 1.19
diff -r1.19 FBPreparedStatement.java
72a73,74
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jdbc/FBStatement.
java,v
retrieving revision 1.14
diff -r1.14 FBStatement.java
210a211,215
< boolean isClosed() {
---
client-java/src/main/org/firebirdsql/jdbc/FBWrappingDataSource.java
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jdbc/FBWrappingDa
taSource.java,v
retrieving revision 1.3
diff -r1.3 FBWrappingDataSource.java
72c72
<
---
< ds = (FBDataSource)mcf.createConnectionFactory(cm);
---
have not looked at the spec. Here is a patch that will implement
prepared statement pooling. The patch is against the 1.0 version of the
driver.
Pooling is off by default and enabled by calling
FBWrappingDataSource.setPreparedStatementPooling(true);
?
client-java/src/main/org/firebirdsql/jca/.FBManagedConnectionFactory.jav
a.marks
? client-java/src/main/org/firebirdsql/jdbc/.FBConnection.java.marks
Index: client-java/src/main/org/firebirdsql/jca/FBManagedConnection.java
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jca/FBManagedConn
ection.java,v
retrieving revision 1.19
diff -r1.19 FBManagedConnection.java
29a30
> import java.util.HashMap;53a55
> import org.firebirdsql.jdbc.FBCallableStatement;54a57
> import org.firebirdsql.jdbc.FBPreparedStatement;97a101,104
> private boolean poolPreparedStatements = false;336a344,361
> private HashMap pooledCallableStatements = null;
> private HashMap pooledPreparedStatements = null;
>
> try {pooledCallableStatements.values().iterator();
> // close pooled statements if applicable
> if (pooledCallableStatements!=null) {
> Iterator it =
> while (it.hasNext()) {((FBCallableStatement)it.next()).destroy();
>
> }pooledPreparedStatements.values().iterator();
> }
> // close pooled statements if applicable
> if (pooledPreparedStatements!=null) {
> Iterator it =
> while (it.hasNext()) {((FBPreparedStatement)it.next()).destroy();
>
> }cleaning up statements", ex);
> }
> } catch (SQLException ex) {
> if (log!=null) log.error("Error
> }1238a1264,1320
>poolPreparedStatements)
> public void setPoolPreparedStatements(boolean
> {getPooledCallableStatement(FBConnection connection, String sql) {
> this.poolPreparedStatements = poolPreparedStatements;
> }
>
> public boolean getPoolPreparedStatements() {
> return mcf.getPoolPreparedStatements();
> }
>
> public synchronized FBCallableStatement
> if (pooledCallableStatements==null) {(FBCallableStatement)pooledCallableStatements.get(sql);
> return null;
> }
> if (pooledCallableStatements.containsKey(sql)) {
> FBCallableStatement stmt =
> pooledCallableStatements.remove(stmt);getPooledPreparedStatement(FBConnection connection, String sql) {
> if (stmt.isClosed()) {
> return null;
> }
> stmt.initializeFromPool(connection);
> return stmt;
> } else {
> return null;
> }
> }
>
> public synchronized FBPreparedStatement
> if (pooledPreparedStatements==null) {(FBPreparedStatement)pooledPreparedStatements.get(sql);
> return null;
> }
> if (pooledPreparedStatements.containsKey(sql)) {
> FBPreparedStatement stmt =
> pooledPreparedStatements.remove(stmt);HashMap();
> if (stmt.isClosed()) {
> return null;
> }
> stmt.initializeFromPool(connection);
> return stmt;
> } else {
> return null;
> }
> }
>
> public void releaseStatement(FBPreparedStatement stmt) {
> if (stmt instanceof FBCallableStatement) {
> if (pooledCallableStatements==null) {
> pooledCallableStatements = new
> }pooledCallableStatements.put(stmt.getSQL(),stmt);
>
> } else {HashMap();
> if (pooledPreparedStatements==null) {
> pooledPreparedStatements = new
> }pooledPreparedStatements.put(stmt.getSQL(),stmt);
>
> }Index:
> }
client-java/src/main/org/firebirdsql/jca/FBManagedConnectionFactory.java
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jca/FBManagedConn
ectionFactory.java,v
retrieving revision 1.19
diff -r1.19 FBManagedConnectionFactory.java
139c139,141
<
---
>145,146c147,148
> private boolean poolPreparedStatements = false;
>
<
<
---
>285a288,296
>
>poolPreparedStatements)
> public void setPoolPreparedStatements(boolean
> {287c298
> this.poolPreparedStatements = poolPreparedStatements;
> }
>
> public boolean getPoolPreparedStatements() {
> return poolPreparedStatements;
> }
<
---
>Index:
client-java/src/main/org/firebirdsql/jca/FBPoolingConnectionManager.java
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jca/FBPoolingConn
ectionManager.java,v
retrieving revision 1.2
diff -r1.2 FBPoolingConnectionManager.java
50c50,51
<
---
>Index:
> private boolean preparedStatementPooling = false;
client-java/src/main/org/firebirdsql/jdbc/FBCallableStatement.java
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jdbc/FBCallableSt
atement.java,v
retrieving revision 1.3
diff -r1.3 FBCallableStatement.java
136c136
<
---
>Index: client-java/src/main/org/firebirdsql/jdbc/FBConnection.java
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jdbc/FBConnection
.java,v
retrieving revision 1.19
diff -r1.19 FBConnection.java
79c79
<
---
>243,244c243,250
< PreparedStatement stmt = new FBPreparedStatement(this, sql);
< activeStatements.add(stmt);
---
> PreparedStatement stmt = null;341,343c347,355
> if (mc.getPoolPreparedStatements()) {
> stmt = mc.getPooledPreparedStatement(this,sql);
> }
> if (stmt==null) {
> stmt = new FBPreparedStatement(this, sql);
> activeStatements.add(stmt);
> }
< CallableStatement stmt = new FBCallableStatement(this, sql);
< activeStatements.add(stmt);
< return stmt;
---
> CallableStatement stmt = null;sql);
> if (mc.getPoolPreparedStatements()) {
> stmt = mc.getPooledCallableStatement(this,
> }1233a1246,1253
> if(stmt==null) {
> stmt = new FBCallableStatement(this, sql);
> activeStatements.add(stmt);
> }
> return stmt;
>mc.getPoolPreparedStatements();
> public boolean getPoolPreparedStatements() {
> return mc==null ? false :
> }Index:
>
> public void releaseStatement(FBPreparedStatement stmt) {
> mc.releaseStatement(stmt);
> }
client-java/src/main/org/firebirdsql/jdbc/FBPreparedStatement.java
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jdbc/FBPreparedSt
atement.java,v
retrieving revision 1.19
diff -r1.19 FBPreparedStatement.java
72a73,74
>75a78
> String sql = null;
> this.sql = sql;145a149,168
>Index: client-java/src/main/org/firebirdsql/jdbc/FBStatement.java
> public void close() throws SQLException {
> if (c.getPoolPreparedStatements()) {
> c.releaseStatement(this);
> } else {
> destroy();
> }
> }
>
>
> public void initializeFromPool(FBConnection connection) {
> this.c = connection;
> }
>
>
>
> public String getSQL() {
> return sql;
> }
>
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jdbc/FBStatement.
java,v
retrieving revision 1.14
diff -r1.14 FBStatement.java
210a211,215
> destroy();243c248
> }
>
>
> public void destroy() throws SQLException {
< boolean isClosed() {
---
> public boolean isClosed() {958a964
>Index:
client-java/src/main/org/firebirdsql/jdbc/FBWrappingDataSource.java
===================================================================
RCS file:
/cvsroot/firebird/client-java/src/main/org/firebirdsql/jdbc/FBWrappingDa
taSource.java,v
retrieving revision 1.3
diff -r1.3 FBWrappingDataSource.java
72c72
<
---
>77a78,87
> public void setPreparedStatementPooling(booleanpoolPreparedStatements)
> {182c192
> mcf.setPoolPreparedStatements(poolPreparedStatements);
> }
>
> public boolean getPreparedStatementPooling()
> {
> return mcf.getPoolPreparedStatements();
> }
>
< ds = (FBDataSource)mcf.createConnectionFactory(cm);
---
> ds =(FBDataSource)mcf.createConnectionFactory(cm);