improve unpooled statement's performance

This commit is contained in:
wenshao
2013-06-08 14:01:52 +08:00
parent 1f45ea28ee
commit 2a48c90450
5 changed files with 68 additions and 36 deletions
@@ -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();
@@ -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();
@@ -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();
}
@@ -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) {
@@ -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) {