From 4bf87dc49795424e939bbb6ec601f96372f560a6 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 17 Dec 2011 23:49:22 +0800 Subject: [PATCH] bug fixed : statement pool --- .../druid/pool/PoolableConnection.java | 18 +-- .../druid/pool/PoolablePreparedStatement.java | 103 +++++++++++++++++- .../basic/PoolableCallableStatementTest.java | 2 +- .../basic/PoolablePreparedStatementTest.java | 2 +- 4 files changed, 111 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/PoolableConnection.java b/src/main/java/com/alibaba/druid/pool/PoolableConnection.java index 74dab89492..4788962b39 100644 --- a/src/main/java/com/alibaba/druid/pool/PoolableConnection.java +++ b/src/main/java/com/alibaba/druid/pool/PoolableConnection.java @@ -203,7 +203,8 @@ public class PoolableConnection implements PooledConnection, Connection { throws SQLException { checkOpen(); - PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M2); + PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M2, resultSetType, + resultSetConcurrency); PreparedStatementHolder stmtHolder = null; @@ -240,7 +241,8 @@ public class PoolableConnection implements PooledConnection, Connection { int resultSetHoldability) throws SQLException { checkOpen(); - PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M3); + PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M3, resultSetType, + resultSetConcurrency, resultSetHoldability); PreparedStatementHolder stmtHolder = null; @@ -279,7 +281,7 @@ public class PoolableConnection implements PooledConnection, Connection { PreparedStatementHolder stmtHolder = null; - PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M4); + PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M4, columnIndexes); if (holder.isPoolPreparedStatements()) { stmtHolder = holder.getStatementPool().get(key); } @@ -313,7 +315,7 @@ public class PoolableConnection implements PooledConnection, Connection { PreparedStatementHolder stmtHolder = null; - PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M5); + PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M5, columnNames); if (holder.isPoolPreparedStatements()) { stmtHolder = holder.getStatementPool().get(key); } @@ -347,7 +349,7 @@ public class PoolableConnection implements PooledConnection, Connection { PreparedStatementHolder stmtHolder = null; - PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M6); + PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M6, autoGeneratedKeys); if (holder.isPoolPreparedStatements()) { stmtHolder = holder.getStatementPool().get(key); } @@ -419,7 +421,8 @@ public class PoolableConnection implements PooledConnection, Connection { PreparedStatementHolder stmtHolder = null; - PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.Precall_2); + PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.Precall_2, resultSetType, + resultSetConcurrency, resultSetHoldability); if (holder.isPoolPreparedStatements()) { stmtHolder = holder.getStatementPool().get(key); } @@ -455,7 +458,8 @@ public class PoolableConnection implements PooledConnection, Connection { PreparedStatementHolder stmtHolder = null; - PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.Precall_3); + PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.Precall_3, resultSetType, + resultSetConcurrency); if (holder.isPoolPreparedStatements()) { stmtHolder = holder.getStatementPool().get(key); } diff --git a/src/main/java/com/alibaba/druid/pool/PoolablePreparedStatement.java b/src/main/java/com/alibaba/druid/pool/PoolablePreparedStatement.java index 306d1fc03c..fb510ac721 100644 --- a/src/main/java/com/alibaba/druid/pool/PoolablePreparedStatement.java +++ b/src/main/java/com/alibaba/druid/pool/PoolablePreparedStatement.java @@ -34,6 +34,7 @@ import java.sql.SQLException; import java.sql.SQLXML; import java.sql.Time; import java.sql.Timestamp; +import java.util.Arrays; import java.util.Calendar; import com.alibaba.druid.pool.PreparedStatementPool.MethodType; @@ -791,7 +792,42 @@ public class PoolablePreparedStatement extends PoolableStatement implements Prep protected final MethodType methodType; + private final int resultSetType; + private final int resultSetConcurrency; + private final int resultSetHoldability; + private final int autoGeneratedKeys; + private final int[] columnIndexes; + private final String[] columnNames; + public PreparedStatementKey(String sql, String catalog, MethodType methodType) throws SQLException{ + this(sql, catalog, methodType, 0, 0, 0, 0, null, null); + } + + public PreparedStatementKey(String sql, String catalog, MethodType methodType, int resultSetType, + int resultSetConcurrency) throws SQLException{ + this(sql, catalog, methodType, resultSetType, resultSetConcurrency, 0, 0, null, null); + } + + public PreparedStatementKey(String sql, String catalog, MethodType methodType, int resultSetType, + int resultSetConcurrency, int resultSetHoldability) throws SQLException{ + this(sql, catalog, methodType, resultSetType, resultSetConcurrency, resultSetHoldability, 0, null, null); + } + + public PreparedStatementKey(String sql, String catalog, MethodType methodType, int autoGeneratedKeys) throws SQLException{ + this(sql, catalog, methodType, 0, 0, 0, autoGeneratedKeys, null, null); + } + + public PreparedStatementKey(String sql, String catalog, MethodType methodType, int[] columnIndexes) throws SQLException{ + this(sql, catalog, methodType, 0, 0, 0, 0, columnIndexes, null); + } + + public PreparedStatementKey(String sql, String catalog, MethodType methodType, String[] columnNames) throws SQLException{ + this(sql, catalog, methodType, 0, 0, 0, 0, null, columnNames); + } + + public PreparedStatementKey(String sql, String catalog, MethodType methodType, int resultSetType, + int resultSetConcurrency, int resultSetHoldability, int autoGeneratedKeys, + int[] columnIndexes, String[] columnNames) throws SQLException{ if (sql == null) { throw new SQLException("sql is null"); } @@ -799,6 +835,24 @@ public class PoolablePreparedStatement extends PoolableStatement implements Prep this.sql = sql; this.catalog = catalog; this.methodType = methodType; + this.resultSetType = resultSetType; + this.resultSetConcurrency = resultSetConcurrency; + this.resultSetHoldability = resultSetHoldability; + this.autoGeneratedKeys = autoGeneratedKeys; + this.columnIndexes = columnIndexes; + this.columnNames = columnNames; + } + + public int getResultSetType() { + return resultSetType; + } + + public int getResultSetConcurrency() { + return resultSetConcurrency; + } + + public int getResultSetHoldability() { + return resultSetHoldability; } public boolean equals(Object object) { @@ -821,19 +875,58 @@ public class PoolablePreparedStatement extends PoolableStatement implements Prep if (this.methodType != that.methodType) { return false; } + + if (this.resultSetType != that.resultSetType) { + return false; + } + + if (this.resultSetConcurrency != that.resultSetConcurrency) { + return false; + } + + if (this.resultSetHoldability != that.resultSetHoldability) { + return false; + } + + if (this.autoGeneratedKeys != that.autoGeneratedKeys) { + return false; + } + + if (!Arrays.equals(columnIndexes, that.columnIndexes)) { + return false; + } + + if (!Arrays.equals(columnNames, that.columnNames)) { + return false; + } return true; } + + public int hashCode() { + final int prime = 31; + int result = 1; + + result = prime * result + ((sql == null) ? 0 : sql.hashCode()); + result = prime * result + ((catalog == null) ? 0 : catalog.hashCode()); + result = prime * result + ((methodType == null) ? 0 : methodType.hashCode()); + + result = prime * result + resultSetConcurrency; + result = prime * result + resultSetHoldability; + result = prime * result + resultSetType; + + result = prime * result + autoGeneratedKeys; + + result = prime * result + Arrays.hashCode(columnIndexes); + result = prime * result + Arrays.hashCode(columnNames); + + return result; + } public String getSql() { return sql; } - public int hashCode() { - int catalogHashCode = catalog == null ? 0 : catalog.hashCode(); - int sqlHashCode = sql.hashCode(); - return sqlHashCode ^ catalogHashCode; - } } } diff --git a/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableCallableStatementTest.java b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableCallableStatementTest.java index 9a07f32f54..00b9f59a3d 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableCallableStatementTest.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableCallableStatementTest.java @@ -29,7 +29,7 @@ public class PoolableCallableStatementTest extends TestCase { protected void setUp() throws Exception { raw = new MockCallableStatement(null, null); stmt = new PoolableCallableStatement(null, - new PreparedStatementHolder(new PreparedStatementKey("", null, null), raw)) { + new PreparedStatementHolder(new PreparedStatementKey("", null, null, 0, 0, 0), raw)) { protected SQLException checkException(Throwable error) throws SQLException { if (error instanceof SQLException) { diff --git a/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolablePreparedStatementTest.java b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolablePreparedStatementTest.java index 492a36051a..e5f097bdd8 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolablePreparedStatementTest.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolablePreparedStatementTest.java @@ -25,7 +25,7 @@ public class PoolablePreparedStatementTest extends TestCase { protected void setUp() throws Exception { raw = new MockPreparedStatement(null, null); stmt = new PoolablePreparedStatement(null, - new PreparedStatementHolder(new PreparedStatementKey("", null, null), raw)) { + new PreparedStatementHolder(new PreparedStatementKey("", null, null, 0, 0, 0), raw)) { protected SQLException checkException(Throwable error) throws SQLException { if (error instanceof SQLException) {