diff --git a/src/main/java/com/alibaba/druid/pool/ConnectionHolder.java b/src/main/java/com/alibaba/druid/pool/ConnectionHolder.java index 2375bd3346..c4a5444059 100644 --- a/src/main/java/com/alibaba/druid/pool/ConnectionHolder.java +++ b/src/main/java/com/alibaba/druid/pool/ConnectionHolder.java @@ -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 statementTrace = new ArrayList(); @@ -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()); } diff --git a/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSourceMBean.java b/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSourceMBean.java index 7ebf1c0ea3..42c679badf 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSourceMBean.java +++ b/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSourceMBean.java @@ -102,6 +102,8 @@ public interface DruidAbstractDataSourceMBean { int getMaxPoolPreparedStatementPerConnectionSize(); + void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize); + String getProperties(); int getRawDriverMinorVersion(); diff --git a/src/main/java/com/alibaba/druid/pool/PreparedStatementPool.java b/src/main/java/com/alibaba/druid/pool/PreparedStatementPool.java index a36027e522..57680d4710 100644 --- a/src/main/java/com/alibaba/druid/pool/PreparedStatementPool.java +++ b/src/main/java/com/alibaba/druid/pool/PreparedStatementPool.java @@ -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 @@ -27,13 +27,15 @@ import com.alibaba.druid.util.LRUCache; public class PreparedStatementPool { private final Map map; + private final DruidAbstractDataSource dataSource; - public PreparedStatementPool(int maxSize){ - if (maxSize < 0) { - map = new HashMap(maxSize); - } else { - map = new LRUCache(maxSize); + public PreparedStatementPool(ConnectionHolder holder){ + this.dataSource = holder.getDataSource(); + int initCapacity = holder.getDataSource().getMaxPoolPreparedStatementPerConnectionSize(); + if (initCapacity <= 0) { + initCapacity = 16; } + map = new LRUCache(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 extends LinkedHashMap { + + private static final long serialVersionUID = 1L; + + public LRUCache(int maxSize){ + super(maxSize); + } + + protected boolean removeEldestEntry(Entry eldest) { + return (size() > dataSource.getMaxPoolPreparedStatementPerConnectionSize()); + } + } } diff --git a/src/main/java/com/alibaba/druid/util/LRUCache.java b/src/main/java/com/alibaba/druid/util/LRUCache.java deleted file mode 100644 index 936898f571..0000000000 --- a/src/main/java/com/alibaba/druid/util/LRUCache.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.alibaba.druid.util; - -import java.util.LinkedHashMap; -import java.util.Map.Entry; - -public class LRUCache extends LinkedHashMap { - - private static final long serialVersionUID = 1L; - protected int maxElements; - - public LRUCache(int maxSize){ - super(maxSize); - this.maxElements = maxSize; - } - - protected boolean removeEldestEntry(Entry eldest) { - return (size() > this.maxElements); - } -} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/basic/PreparedStatementKeyTest.java b/src/test/java/com/alibaba/druid/bvt/pool/basic/PreparedStatementKeyTest.java index 4da119e45d..e0d7de9792 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/basic/PreparedStatementKeyTest.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/basic/PreparedStatementKeyTest.java @@ -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);