mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
完善监控
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@922 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -40,7 +40,6 @@ public final class ConnectionHolder {
|
||||
private long lastActiveTimeMillis;
|
||||
private long useCount = 0;
|
||||
|
||||
private final boolean poolPreparedStatements;
|
||||
private PreparedStatementPool statementPool;
|
||||
|
||||
private final List<Statement> statementTrace = new ArrayList<Statement>();
|
||||
@@ -48,15 +47,10 @@ public final class ConnectionHolder {
|
||||
public ConnectionHolder(DruidAbstractDataSource dataSource, Connection conn){
|
||||
this.dataSource = dataSource;
|
||||
this.conn = conn;
|
||||
this.poolPreparedStatements = dataSource.isPoolPreparedStatements();
|
||||
this.connecttimeMillis = System.currentTimeMillis();
|
||||
this.lastActiveTimeMillis = connecttimeMillis;
|
||||
|
||||
if (this.poolPreparedStatements) {
|
||||
statementPool = new PreparedStatementPool(dataSource.getMaxPoolPreparedStatementPerConnectionSize());
|
||||
} else {
|
||||
statementPool = null;
|
||||
}
|
||||
statementPool = null;
|
||||
}
|
||||
|
||||
public long getLastActiveTimeMillis() {
|
||||
@@ -85,7 +79,7 @@ public final class ConnectionHolder {
|
||||
|
||||
public PreparedStatementPool getStatementPool() {
|
||||
if (statementPool == null) {
|
||||
statementPool = new PreparedStatementPool(dataSource.getMaxPoolPreparedStatementPerConnectionSize());
|
||||
statementPool = new PreparedStatementPool(this);
|
||||
}
|
||||
return statementPool;
|
||||
}
|
||||
@@ -95,7 +89,7 @@ public final class ConnectionHolder {
|
||||
}
|
||||
|
||||
public boolean isPoolPreparedStatements() {
|
||||
return poolPreparedStatements;
|
||||
return dataSource.isPoolPreparedStatements();
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
@@ -142,7 +136,8 @@ public final class ConnectionHolder {
|
||||
buf.append("\"");
|
||||
}
|
||||
|
||||
if (this.getStatementPool() != null) {
|
||||
PreparedStatementPool statmentPool = this.getStatementPool();
|
||||
if (statmentPool != null && statmentPool.getMap().size() > 0) {
|
||||
buf.append("\", CachedStatementCount:");
|
||||
buf.append(this.getStatementPool().getMap().size());
|
||||
}
|
||||
|
||||
@@ -102,6 +102,8 @@ public interface DruidAbstractDataSourceMBean {
|
||||
|
||||
int getMaxPoolPreparedStatementPerConnectionSize();
|
||||
|
||||
void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize);
|
||||
|
||||
String getProperties();
|
||||
|
||||
int getRawDriverMinorVersion();
|
||||
|
||||
@@ -15,11 +15,11 @@
|
||||
*/
|
||||
package com.alibaba.druid.pool;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.alibaba.druid.pool.PoolablePreparedStatement.PreparedStatementKey;
|
||||
import com.alibaba.druid.util.LRUCache;
|
||||
|
||||
/**
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
@@ -27,13 +27,15 @@ import com.alibaba.druid.util.LRUCache;
|
||||
public class PreparedStatementPool {
|
||||
|
||||
private final Map<PreparedStatementKey, PreparedStatementHolder> map;
|
||||
private final DruidAbstractDataSource dataSource;
|
||||
|
||||
public PreparedStatementPool(int maxSize){
|
||||
if (maxSize < 0) {
|
||||
map = new HashMap<PreparedStatementKey, PreparedStatementHolder>(maxSize);
|
||||
} else {
|
||||
map = new LRUCache<PreparedStatementKey, PreparedStatementHolder>(maxSize);
|
||||
public PreparedStatementPool(ConnectionHolder holder){
|
||||
this.dataSource = holder.getDataSource();
|
||||
int initCapacity = holder.getDataSource().getMaxPoolPreparedStatementPerConnectionSize();
|
||||
if (initCapacity <= 0) {
|
||||
initCapacity = 16;
|
||||
}
|
||||
map = new LRUCache<PreparedStatementKey, PreparedStatementHolder>(initCapacity);
|
||||
}
|
||||
|
||||
public static enum MethodType {
|
||||
@@ -46,7 +48,7 @@ public class PreparedStatementPool {
|
||||
if (holder != null) {
|
||||
holder.incrementReusedCount();
|
||||
}
|
||||
|
||||
|
||||
return holder;
|
||||
}
|
||||
|
||||
@@ -59,4 +61,16 @@ public class PreparedStatementPool {
|
||||
return map;
|
||||
}
|
||||
|
||||
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public LRUCache(int maxSize){
|
||||
super(maxSize);
|
||||
}
|
||||
|
||||
protected boolean removeEldestEntry(Entry<K, V> eldest) {
|
||||
return (size() > dataSource.getMaxPoolPreparedStatementPerConnectionSize());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.mock.MockPreparedStatement;
|
||||
import com.alibaba.druid.pool.ConnectionHolder;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.PoolablePreparedStatement.PreparedStatementKey;
|
||||
import com.alibaba.druid.pool.PreparedStatementHolder;
|
||||
import com.alibaba.druid.pool.PreparedStatementPool;
|
||||
@@ -95,8 +97,9 @@ public class PreparedStatementKeyTest extends TestCase {
|
||||
}
|
||||
|
||||
public void test_contains() throws Exception {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
PreparedStatementKey k1 = new PreparedStatementKey("x1", "c1", MethodType.M1);
|
||||
PreparedStatementPool pool = new PreparedStatementPool(10);
|
||||
PreparedStatementPool pool = new PreparedStatementPool(new ConnectionHolder(dataSource, null));
|
||||
MockPreparedStatement raw = new MockPreparedStatement(null, null);
|
||||
pool.put(new PreparedStatementHolder(k1, raw));
|
||||
Assert.assertTrue(pool.get(k1) != null);
|
||||
|
||||
Reference in New Issue
Block a user