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@566 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -85,7 +85,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
// store
|
||||
private ConnectionHolder[] connections;
|
||||
private volatile int poolingCount = 0;
|
||||
private int poolingCount = 0;
|
||||
private int activeCount = 0;
|
||||
|
||||
// threads
|
||||
@@ -532,9 +532,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
connections[lastIndex] = null;
|
||||
poolingCount--;
|
||||
|
||||
if ((minIdle == 0 && poolingCount == 0) || poolingCount <= minIdle - 1) {
|
||||
lowWater.signal();
|
||||
}
|
||||
// if ((minIdle == 0 && poolingCount == 0) || poolingCount <= minIdle - 1) {
|
||||
// lowWater.signal();
|
||||
// }
|
||||
// if (count == 0) {
|
||||
// lowWater.signal();
|
||||
// }
|
||||
@@ -580,9 +580,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
// if (lastIndex == minIdle - 1) {
|
||||
// lowWater.signal();
|
||||
// }
|
||||
if ((minIdle == 0 && poolingCount == 0) || poolingCount <= minIdle - 1) {
|
||||
lowWater.signal();
|
||||
}
|
||||
// if ((minIdle == 0 && poolingCount == 0) || poolingCount <= minIdle - 1) {
|
||||
// lowWater.signal();
|
||||
// }
|
||||
|
||||
return last;
|
||||
}
|
||||
@@ -641,7 +641,12 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
// addLast
|
||||
lock.lock();
|
||||
try {
|
||||
if (poolingCount == 0 && minIdle == 0 && lock.getWaitQueueLength(notEmpty) > 0) {
|
||||
if (lock.getWaitQueueLength(notEmpty) == 0) {
|
||||
lowWater.await();
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((poolingCount == 0 && minIdle == 0) || lock.getWaitQueueLength(notEmpty) > 0) {
|
||||
// not wait
|
||||
} else if (poolingCount >= minIdle) {
|
||||
lowWater.await();
|
||||
@@ -649,7 +654,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
// 防止创建超过maxActive数量的连接
|
||||
if (activeCount >= maxActive) {
|
||||
if (activeCount + poolingCount >= maxIdle) {
|
||||
lowWater.await();
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -38,8 +38,8 @@ public class TestIdle3_Concurrent_MaxActive extends TestCase {
|
||||
|
||||
public void test_idle2() throws Exception {
|
||||
|
||||
ManagementFactory.getPlatformMBeanServer().registerMBean(dataSource,
|
||||
new ObjectName("com.alibaba:type=DataSource"));
|
||||
// ManagementFactory.getPlatformMBeanServer().registerMBean(dataSource,
|
||||
// new ObjectName("com.alibaba:type=DataSource"));
|
||||
|
||||
// 第一次创建连接
|
||||
{
|
||||
@@ -54,14 +54,14 @@ public class TestIdle3_Concurrent_MaxActive extends TestCase {
|
||||
|
||||
conn.close();
|
||||
Assert.assertEquals(0, dataSource.getDestroyCount());
|
||||
//Assert.assertEquals(2, driver.getConnections().size());
|
||||
//Assert.assertEquals(2, dataSource.getCreateCount());
|
||||
Assert.assertEquals(1, driver.getConnections().size());
|
||||
Assert.assertEquals(1, dataSource.getCreateCount());
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
}
|
||||
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
concurrent(20);
|
||||
Assert.assertEquals(14, dataSource.getPoolingCount());
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
concurrent(200);
|
||||
Assert.assertEquals("" + i, dataSource.getMaxActive(), dataSource.getPoolingCount());
|
||||
dataSource.shrink();
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ public class TestIdle3_Concurrent_MaxActive extends TestCase {
|
||||
try {
|
||||
startLatch.await();
|
||||
Connection conn = dataSource.getConnection();
|
||||
long millis = new Random().nextInt(10) + 10;
|
||||
long millis = new Random().nextInt(5) + 5;
|
||||
Thread.sleep(millis);
|
||||
conn.close();
|
||||
} catch (Exception e) {
|
||||
|
||||
Reference in New Issue
Block a user