From 2a48c904507322674095dde48e825036cd90c8e4 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 8 Jun 2013 14:01:52 +0800 Subject: [PATCH] improve unpooled statement's performance --- .../druid/pool/DruidPooledConnection.java | 4 +- .../pool/DruidPooledPreparedStatement.java | 60 +++++++++++-------- .../druid/bvt/pool/basic/ConnectionTest4.java | 5 +- .../basic/PoolableCallableStatementTest.java | 16 ++++- .../basic/PoolablePreparedStatementTest.java | 19 ++++-- 5 files changed, 68 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/DruidPooledConnection.java b/src/main/java/com/alibaba/druid/pool/DruidPooledConnection.java index 1a858d93e7..1b995eaa5d 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidPooledConnection.java +++ b/src/main/java/com/alibaba/druid/pool/DruidPooledConnection.java @@ -145,7 +145,7 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql. return; } - if (holder.isPoolPreparedStatements()) { + if (stmt.isPooled()) { try { rawStatement.clearParameters(); } catch (SQLException ex) { @@ -159,7 +159,7 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql. } stmt.getPreparedStatementHolder().decrementInUseCount(); - if (holder.isPoolPreparedStatements()) { + if (stmt.isPooled() && holder.isPoolPreparedStatements()) { holder.getStatementPool().put(stmt.getPreparedStatementHolder()); stmt.clearResultSet(); diff --git a/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java b/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java index e6344bba67..285e14c569 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java +++ b/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java @@ -63,6 +63,8 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement private int currentQueryTimeout; private int currentFetchDirection; private int currentFetchSize; + + private boolean pooled = false; public DruidPooledPreparedStatement(DruidPooledConnection conn, PreparedStatementHolder holder) throws SQLException{ super(conn, holder.getStatement()); @@ -70,35 +72,39 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement this.holder = holder; this.sql = holder.getKey().sql; + pooled = conn.getConnectionHolder().isPoolPreparedStatements(); // Remember the defaults - try { - defaultMaxFieldSize = stmt.getMaxFieldSize(); - } catch (SQLException e) { - LOG.error("getMaxFieldSize error", e); - } - try { - defaultMaxRows = stmt.getMaxRows(); - } catch (SQLException e) { - LOG.error("getMaxRows error", e); - } + if (pooled) { + try { + defaultMaxFieldSize = stmt.getMaxFieldSize(); + } catch (SQLException e) { + LOG.error("getMaxFieldSize error", e); + } - try { - defaultQueryTimeout = stmt.getQueryTimeout(); - } catch (SQLException e) { - LOG.error("getMaxRows error", e); - } + try { + defaultMaxRows = stmt.getMaxRows(); + } catch (SQLException e) { + LOG.error("getMaxRows error", e); + } - try { - defaultFetchDirection = stmt.getFetchDirection(); - } catch (SQLException e) { - LOG.error("getFetchDirection error", e); - } + try { + defaultQueryTimeout = stmt.getQueryTimeout(); + } catch (SQLException e) { + LOG.error("getMaxRows error", e); + } - try { - defaultFetchSize = stmt.getFetchSize(); - } catch (SQLException e) { - LOG.error("getFetchSize error", e); + try { + defaultFetchDirection = stmt.getFetchDirection(); + } catch (SQLException e) { + LOG.error("getFetchDirection error", e); + } + + try { + defaultFetchSize = stmt.getFetchSize(); + } catch (SQLException e) { + LOG.error("getFetchSize error", e); + } } currentMaxFieldSize = defaultMaxFieldSize; @@ -165,7 +171,7 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement boolean connectionClosed = this.conn.isClosed(); // Reset the defaults - if (!connectionClosed) { + if (pooled && !connectionClosed) { try { if (defaultMaxFieldSize != currentMaxFieldSize) { stmt.setMaxFieldSize(defaultMaxFieldSize); @@ -194,6 +200,10 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement conn.closePoolableStatement(this); } + + public boolean isPooled() { + return pooled; + } void closeInternal() throws SQLException { super.close(); diff --git a/src/test/java/com/alibaba/druid/bvt/pool/basic/ConnectionTest4.java b/src/test/java/com/alibaba/druid/bvt/pool/basic/ConnectionTest4.java index e61fe21727..dc74b89f38 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/basic/ConnectionTest4.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/basic/ConnectionTest4.java @@ -22,9 +22,10 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import junit.framework.Assert; import junit.framework.TestCase; +import org.junit.Assert; + import com.alibaba.druid.mock.MockDriver; import com.alibaba.druid.mock.MockPreparedStatement; import com.alibaba.druid.pool.DruidDataSource; @@ -264,7 +265,7 @@ public class ConnectionTest4 extends TestCase { PreparedStatement stmt = conn.prepareStatement("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT); - Assert.assertEquals(raw, stmt.unwrap(MockPreparedStatement.class)); + Assert.assertSame(raw, stmt.unwrap(MockPreparedStatement.class)); stmt.close(); } 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 49b29b825c..7d521eac8b 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 @@ -32,19 +32,29 @@ import junit.framework.Assert; import junit.framework.TestCase; import com.alibaba.druid.mock.MockCallableStatement; +import com.alibaba.druid.mock.MockConnection; +import com.alibaba.druid.pool.DruidConnectionHolder; +import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidPooledCallableStatement; +import com.alibaba.druid.pool.DruidPooledConnection; import com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey; import com.alibaba.druid.pool.PreparedStatementHolder; public class PoolableCallableStatementTest extends TestCase { - protected MockCallableStatement raw; + private DruidDataSource dataSource = new DruidDataSource(); + protected DruidPooledConnection conn; + protected MockCallableStatement raw; protected DruidPooledCallableStatement stmt; protected void setUp() throws Exception { + MockConnection mockConn = new MockConnection(); + DruidConnectionHolder connHolder = new DruidConnectionHolder(dataSource, mockConn); + conn = new DruidPooledConnection(connHolder); raw = new MockCallableStatement(null, null); - stmt = new DruidPooledCallableStatement(null, - new PreparedStatementHolder(new PreparedStatementKey("", null, null, 0, 0, 0), raw)) { + stmt = new DruidPooledCallableStatement(conn, 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 46804e7704..e5fb07d887 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 @@ -24,23 +24,34 @@ import java.sql.Timestamp; import java.sql.Types; import java.util.Arrays; -import junit.framework.Assert; import junit.framework.TestCase; +import org.junit.Assert; + +import com.alibaba.druid.mock.MockConnection; import com.alibaba.druid.mock.MockPreparedStatement; +import com.alibaba.druid.pool.DruidConnectionHolder; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.pool.DruidPooledConnection; import com.alibaba.druid.pool.DruidPooledPreparedStatement; import com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey; import com.alibaba.druid.pool.PreparedStatementHolder; public class PoolablePreparedStatementTest extends TestCase { - protected MockPreparedStatement raw; + protected MockPreparedStatement raw; protected DruidPooledPreparedStatement stmt; protected void setUp() throws Exception { + DruidDataSource dataSource = new DruidDataSource(); + MockConnection mockConn = new MockConnection(); + DruidConnectionHolder connHolder = new DruidConnectionHolder(dataSource, mockConn); + DruidPooledConnection conn = new DruidPooledConnection(connHolder); + raw = new MockPreparedStatement(null, null); - stmt = new DruidPooledPreparedStatement(null, - new PreparedStatementHolder(new PreparedStatementKey("", null, null, 0, 0, 0), raw)) { + stmt = new DruidPooledPreparedStatement(conn, new PreparedStatementHolder(new PreparedStatementKey("", null, + null, 0, 0, + 0), raw)) { protected SQLException checkException(Throwable error) throws SQLException { if (error instanceof SQLException) {