From 4f3831f32ce226fee02f97008530d396d637b9ea Mon Sep 17 00:00:00 2001 From: wenshao Date: Fri, 16 Sep 2011 07:40:47 +0000 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84mock=E5=92=8C=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.alibabatech.com/svn/druid/trunk@808 b9813039-fb51-4c41-a8b9-e21c2acb5095 --- .../druid/mock/MockCallableStatement.java | 4 + .../druid/mock/MockPreparedStatement.java | 12 ++ .../druid/pool/PoolableConnection.java | 8 +- .../basic/PoolablePreparedStatementTest.java | 29 ++++ .../pool/basic/PoolableStatementTest2.java | 126 ++++++++++++++++++ 5 files changed, 177 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/alibaba/druid/mock/MockCallableStatement.java b/src/main/java/com/alibaba/druid/mock/MockCallableStatement.java index 699a39ec68..d0a520986f 100644 --- a/src/main/java/com/alibaba/druid/mock/MockCallableStatement.java +++ b/src/main/java/com/alibaba/druid/mock/MockCallableStatement.java @@ -70,6 +70,10 @@ public class MockCallableStatement extends MockPreparedStatement implements Call @Override public boolean wasNull() throws SQLException { + if (closed) { + throw new SQLException(); + } + return wasNull; } diff --git a/src/main/java/com/alibaba/druid/mock/MockPreparedStatement.java b/src/main/java/com/alibaba/druid/mock/MockPreparedStatement.java index 9c6e5ca590..c8b4141470 100644 --- a/src/main/java/com/alibaba/druid/mock/MockPreparedStatement.java +++ b/src/main/java/com/alibaba/druid/mock/MockPreparedStatement.java @@ -60,6 +60,10 @@ public class MockPreparedStatement extends MockStatement implements PreparedStat @Override public ResultSet executeQuery() throws SQLException { + if (closed) { + throw new SQLException(); + } + if (mockConnection != null && mockConnection.getDriver() != null) { return mockConnection.getDriver().createResultSet(this); } @@ -73,6 +77,10 @@ public class MockPreparedStatement extends MockStatement implements PreparedStat @Override public int executeUpdate() throws SQLException { + if (closed) { + throw new SQLException(); + } + return 0; } @@ -163,6 +171,10 @@ public class MockPreparedStatement extends MockStatement implements PreparedStat @Override public void clearParameters() throws SQLException { + if (closed) { + throw new SQLException(); + } + parameters.clear(); } diff --git a/src/main/java/com/alibaba/druid/pool/PoolableConnection.java b/src/main/java/com/alibaba/druid/pool/PoolableConnection.java index f413b0272b..76fbaa9191 100644 --- a/src/main/java/com/alibaba/druid/pool/PoolableConnection.java +++ b/src/main/java/com/alibaba/druid/pool/PoolableConnection.java @@ -81,8 +81,12 @@ public class PoolableConnection implements PooledConnection, Connection { void closePoolableStatement(PoolablePreparedStatement stmt) throws SQLException { PreparedStatement rawStatement = stmt.getRawPreparedStatement(); - rawStatement.clearParameters(); - + try { + rawStatement.clearParameters(); + } catch (SQLException ex) { + LOG.error("clear parameter error", ex); + } + if (holder.isPoolPreparedStatements()) { this.holder.getStatementPool().put(stmt); stmt.clearResultSet(); 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 0d7f7e106b..ac62ffeb2f 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 @@ -323,4 +323,33 @@ public class PoolablePreparedStatementTest extends TestCase { } } + public void test_setRef() throws Exception { + + stmt.setRef(1, null); + + { + SQLException error = null; + try { + stmt.setRef(0, null); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + } + + public void test_setArray() throws Exception { + + stmt.setArray(1, null); + + { + SQLException error = null; + try { + stmt.setArray(0, null); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + } } diff --git a/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest2.java b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest2.java index da5062ff7e..359bbea48e 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest2.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest2.java @@ -1,5 +1,6 @@ package com.alibaba.druid.bvt.pool.basic; +import java.sql.CallableStatement; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; @@ -804,5 +805,130 @@ public class PoolableStatementTest2 extends TestCase { conn.close(); } + public void test_wasNull() throws Exception { + Connection conn = dataSource.getConnection(); + CallableStatement stmt = conn.prepareCall("SELELCT 1"); + + stmt.wasNull(); + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.wasNull(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } + public void test_executeQuery() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("SELELCT 1"); + + stmt.executeQuery(); + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.executeQuery(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } + + public void test_executeUpdate_4() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("SELELCT 1"); + + stmt.executeQuery(); + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.executeUpdate(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } + + public void test_execute_3() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("SELELCT 1"); + + stmt.execute(); + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.execute(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } + + + public void test_clearParameters() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("SELELCT 1"); + + stmt.clearParameters(); + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.clearParameters(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } + + public void test_addBatch_1() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("SELELCT 1"); + + stmt.addBatch(); + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.addBatch(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } }