改进PreparedStatementCache

git-svn-id: http://code.alibabatech.com/svn/druid/trunk@915 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-09-28 23:45:54 +00:00
parent bdd0671d85
commit 38fcc483f6
4 changed files with 30 additions and 7 deletions
@@ -53,7 +53,7 @@ public final class ConnectionHolder {
this.lastActiveTimeMillis = connecttimeMillis;
if (this.poolPreparedStatements) {
statementPool = new PreparedStatementPool();
statementPool = new PreparedStatementPool(dataSource.getMaxPoolPreparedStatementPerConnectionSize());
} else {
statementPool = null;
}
@@ -118,7 +118,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
private boolean testOnReturn = DEFAULT_TEST_ON_RETURN;
private boolean testWhileIdle = DEFAULT_WHILE_IDLE;
protected boolean poolPreparedStatements = false;
protected int maxPoolPreparedStatementPerConnectionSize = -1;
protected int maxPoolPreparedStatementPerConnectionSize = 10;
protected boolean inited = false;
@@ -192,11 +192,11 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
public Histogram getTransactionHistogram() {
return transactionHistogram;
}
public void incrementPreparedStatementCount() {
reusePreparedStatement.incrementAndGet();
}
public long getReusePreparedStatement() {
return reusePreparedStatement.get();
}
@@ -16,16 +16,20 @@
package com.alibaba.druid.pool;
import java.sql.PreparedStatement;
import java.util.HashMap;
import com.alibaba.druid.pool.PoolablePreparedStatement.PreparedStatementKey;
import com.alibaba.druid.util.LRUCache;
/**
* @author wenshao<szujobs@hotmail.com>
*/
public class PreparedStatementPool {
private HashMap<PreparedStatementKey, PreparedStatement> map = new HashMap<PreparedStatementKey, PreparedStatement>();
private final LRUCache<PreparedStatementKey, PreparedStatement> map;
public PreparedStatementPool(int maxSize){
map = new LRUCache<PreparedStatementKey, PreparedStatement>(maxSize);
}
public static enum MethodType {
M1, M2, M3, M4, M5, M6, Precall_1, Precall_2, Precall_3
@@ -40,7 +44,7 @@ public class PreparedStatementPool {
map.put(key, poolableStatement.getRawPreparedStatement());
}
public HashMap<PreparedStatementKey, PreparedStatement> getMap() {
public LRUCache<PreparedStatementKey, PreparedStatement> getMap() {
return map;
}
@@ -0,0 +1,19 @@
package com.alibaba.druid.util;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
private static final long serialVersionUID = 1L;
protected int maxElements;
public LRUCache(int maxSize){
super(maxSize);
this.maxElements = maxSize;
}
protected boolean removeEldestEntry(Entry<K, V> eldest) {
return (size() > this.maxElements);
}
}