修正动态修改PoolPreparedStatement参数的错误

This commit is contained in:
wenshao
2011-11-16 10:34:32 +08:00
parent 5b6b22c71d
commit f6a94cd47b
3 changed files with 61 additions and 12 deletions
@@ -155,7 +155,9 @@ public class PoolableConnection implements PooledConnection, Connection {
try {
stmtHolder = new PreparedStatementHolder(key, conn.prepareStatement(sql));
holder.getDataSource().incrementPreparedStatementCount();
holder.getStatementPool().put(stmtHolder);
if (holder.isPoolPreparedStatements()) {
holder.getStatementPool().put(stmtHolder);
}
} catch (SQLException ex) {
handleException(ex);
}
@@ -190,7 +192,9 @@ public class PoolableConnection implements PooledConnection, Connection {
stmtHolder = new PreparedStatementHolder(key, conn.prepareStatement(sql, resultSetType,
resultSetConcurrency));
holder.getDataSource().incrementPreparedStatementCount();
holder.getStatementPool().put(stmtHolder);
if (holder.isPoolPreparedStatements()) {
holder.getStatementPool().put(stmtHolder);
}
} catch (SQLException ex) {
handleException(ex);
}
@@ -226,7 +230,9 @@ public class PoolableConnection implements PooledConnection, Connection {
resultSetConcurrency,
resultSetHoldability));
holder.getDataSource().incrementPreparedStatementCount();
holder.getStatementPool().put(stmtHolder);
if (holder.isPoolPreparedStatements()) {
holder.getStatementPool().put(stmtHolder);
}
} catch (SQLException ex) {
handleException(ex);
}
@@ -258,7 +264,9 @@ public class PoolableConnection implements PooledConnection, Connection {
try {
stmtHolder = new PreparedStatementHolder(key, conn.prepareStatement(sql, columnIndexes));
holder.getDataSource().incrementPreparedStatementCount();
holder.getStatementPool().put(stmtHolder);
if (holder.isPoolPreparedStatements()) {
holder.getStatementPool().put(stmtHolder);
}
} catch (SQLException ex) {
handleException(ex);
}
@@ -290,7 +298,9 @@ public class PoolableConnection implements PooledConnection, Connection {
try {
stmtHolder = new PreparedStatementHolder(key, conn.prepareStatement(sql, columnNames));
holder.getDataSource().incrementPreparedStatementCount();
holder.getStatementPool().put(stmtHolder);
if (holder.isPoolPreparedStatements()) {
holder.getStatementPool().put(stmtHolder);
}
} catch (SQLException ex) {
handleException(ex);
}
@@ -322,7 +332,9 @@ public class PoolableConnection implements PooledConnection, Connection {
try {
stmtHolder = new PreparedStatementHolder(key, conn.prepareStatement(sql, autoGeneratedKeys));
holder.getDataSource().incrementPreparedStatementCount();
holder.getStatementPool().put(stmtHolder);
if (holder.isPoolPreparedStatements()) {
holder.getStatementPool().put(stmtHolder);
}
} catch (SQLException ex) {
handleException(ex);
}
@@ -357,7 +369,9 @@ public class PoolableConnection implements PooledConnection, Connection {
try {
stmtHolder = new PreparedStatementHolder(key, conn.prepareCall(sql));
holder.getDataSource().incrementPreparedStatementCount();
holder.getStatementPool().put(stmtHolder);
if (holder.isPoolPreparedStatements()) {
holder.getStatementPool().put(stmtHolder);
}
} catch (SQLException ex) {
handleException(ex);
}
@@ -392,7 +406,9 @@ public class PoolableConnection implements PooledConnection, Connection {
resultSetConcurrency,
resultSetHoldability));
holder.getDataSource().incrementPreparedStatementCount();
holder.getStatementPool().put(stmtHolder);
if (holder.isPoolPreparedStatements()) {
holder.getStatementPool().put(stmtHolder);
}
} catch (SQLException ex) {
handleException(ex);
}
@@ -425,7 +441,9 @@ public class PoolableConnection implements PooledConnection, Connection {
stmtHolder = new PreparedStatementHolder(key,
conn.prepareCall(sql, resultSetType, resultSetConcurrency));
holder.getDataSource().incrementPreparedStatementCount();
holder.getStatementPool().put(stmtHolder);
if (holder.isPoolPreparedStatements()) {
holder.getStatementPool().put(stmtHolder);
}
} catch (SQLException ex) {
handleException(ex);
}
@@ -15,6 +15,8 @@
*/
package com.alibaba.druid.pool;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
@@ -52,7 +54,13 @@ public class PreparedStatementPool {
return holder;
}
public void put(PreparedStatementHolder holder) {
public void put(PreparedStatementHolder holder) throws SQLException {
PreparedStatement stmt = holder.getStatement();
if (stmt.isClosed()) {
return;
}
PreparedStatementKey key = holder.getKey();
PreparedStatementHolder oldHolder = map.put(key, holder);
if (oldHolder != null) {
@@ -34,7 +34,7 @@ public class TestPoolPreparedStatement2 extends TestCase {
dataSource.setTestOnBorrow(false);
dataSource.setValidationQuery("SELECT 1");
dataSource.setFilters("stat");
dataSource.setPoolPreparedStatements(true);
dataSource.setPoolPreparedStatements(false);
dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
}
@@ -44,7 +44,30 @@ public class TestPoolPreparedStatement2 extends TestCase {
}
public void test_stmtCache() throws Exception {
for (int j = 0; j < 10; ++j) {
for (int i = 0; i < 10; ++i) {
Connection conn = dataSource.getConnection();
String sql = "SELECT" + i;
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.execute();
stmt.close();
conn.close();
}
}
dataSource.setPoolPreparedStatements(true);
for (int j = 0; j < 10; ++j) {
for (int i = 0; i < 10; ++i) {
Connection conn = dataSource.getConnection();
String sql = "SELECT" + i;
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.execute();
stmt.close();
conn.close();
}
}
for (int i = 0; i < 1000 * 100 * 1; ++i) {
Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT " + i);