user clearDefines optimize Oracle PrepareStatement Cache memoery use

This commit is contained in:
wenshao
2011-12-29 23:04:03 +08:00
parent c54590db4a
commit 1352771e3f
9 changed files with 98 additions and 19 deletions
@@ -1279,8 +1279,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
if (entryIndex++ != 0) {
buf.append(",");
}
buf.append("\n\t\t{reuseCount:");
buf.append(entry.getValue().getReusedCount());
buf.append("\n\t\t{hitCount:");
buf.append(entry.getValue().getHitCount());
buf.append(",sql:\"");
buf.append(entry.getKey().getSql());
buf.append("\"");
@@ -56,18 +56,8 @@ public interface DruidDataSourceMBean extends DruidAbstractDataSourceMBean {
void setPoolPreparedStatements(boolean poolPreparedStatements);
/**
* ActiveCount峰值
*
* @return
*/
int getActivePeak();
/**
* PoolingCount峰值
*
* @return
*/
int getPoolingPeak();
Date getActivePeakTime();
@@ -0,0 +1,17 @@
package com.alibaba.druid.pool;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import oracle.jdbc.driver.OracleStatement;
public class OracleUtils {
public static void clearDefines(PoolablePreparedStatement stmt) throws SQLException {
PreparedStatement raw = stmt.getRawPreparedStatement();
if (raw instanceof OracleStatement) {
OracleStatement oracleStmt = (OracleStatement) raw;
oracleStmt.clearDefines();
}
}
}
@@ -96,6 +96,10 @@ public class PoolableConnection implements PooledConnection, Connection {
throw new SQLException("Error", t);
}
private boolean isOracle() {
return "oracle".equals(holder.getDataSource().getDbType());
}
void closePoolableStatement(PoolablePreparedStatement stmt) throws SQLException {
if (this.holder == null) {
@@ -118,6 +122,10 @@ public class PoolableConnection implements PooledConnection, Connection {
stmt.clearResultSet();
holder.removeTrace(stmt);
stmt.setClosed(true); // soft set close
if (stmt.getHitCount() == 0 && isOracle()) {
OracleUtils.clearDefines(stmt);
}
} else {
stmt.closeInternal();
holder.getDataSource().incrementClosedPreparedStatementCount();
@@ -81,6 +81,10 @@ public class PoolablePreparedStatement extends PoolableStatement implements Prep
public PreparedStatementHolder getPreparedStatementHolder() {
return holder;
}
public int getHitCount() {
return holder.getHitCount();
}
public void setFetchSize(int rows) throws SQLException {
currentFetchSize = rows;
@@ -26,7 +26,7 @@ public final class PreparedStatementHolder {
private final PreparedStatementKey key;
private final PreparedStatement statement;
private int useCount = 0;
private int hitCount = 0;
public PreparedStatementHolder(PreparedStatementKey key, PreparedStatement stmt){
this.key = key;
@@ -41,12 +41,12 @@ public final class PreparedStatementHolder {
return key;
}
public void incrementReusedCount() {
useCount++;
public void incrementHitCount() {
hitCount++;
}
public int getReusedCount() {
return useCount;
public int getHitCount() {
return hitCount;
}
}
@@ -49,7 +49,7 @@ public class PreparedStatementPool {
PreparedStatementHolder holder = map.get(key);
if (holder != null) {
holder.incrementReusedCount();
holder.incrementHitCount();
dataSource.incrementCachedPreparedStatementHitCount();
} else {
dataSource.incrementCachedPreparedStatementMissCount();
@@ -70,7 +70,7 @@ public class PreparedStatementPool {
if (oldHolder != null) {
dataSource.closePreapredStatement(oldHolder);
} else {
if (holder.getReusedCount() == 0) {
if (holder.getHitCount() == 0) {
dataSource.incrementCachedPreparedStatementCount();
}
}