mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for GetConnectionTimeoutException
This commit is contained in:
@@ -215,7 +215,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
|
||||
protected boolean useOracleImplicitCache = true;
|
||||
|
||||
protected final ReentrantLock lock = new ReentrantLock();
|
||||
protected final ReentrantLock lock = new ReentrantLock(true);
|
||||
|
||||
protected int modCount = 0;
|
||||
|
||||
|
||||
@@ -779,7 +779,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
ConnectionHolder pollLast(long timeout, TimeUnit unit) throws InterruptedException, SQLException {
|
||||
long estimate = unit.toNanos(timeout);
|
||||
|
||||
for (;;) {
|
||||
for (int i = 0;;++i) {
|
||||
if (poolingCount == 0) {
|
||||
empty.signal(); // send signal to CreateThread create connection
|
||||
|
||||
@@ -820,7 +820,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
if (createError != null) {
|
||||
throw new GetConnectionTimeoutException(createError);
|
||||
} else {
|
||||
throw new GetConnectionTimeoutException();
|
||||
throw new GetConnectionTimeoutException("loopWaitCount " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -928,7 +928,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
// 必须存在线程等待,才创建连接
|
||||
if (poolingCount >= notEmptyWaitThreadCount) {
|
||||
empty.await();
|
||||
continue;
|
||||
}
|
||||
|
||||
// 防止创建超过maxActive数量的连接
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class TestConnectTimeout extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
|
||||
dataSource.setUsername("xxx1");
|
||||
dataSource.setPassword("ppp");
|
||||
dataSource.setUrl("jdbc:mock:xx");
|
||||
dataSource.setFilters("stat");
|
||||
dataSource.setMaxOpenPreparedStatements(30);
|
||||
dataSource.setMaxActive(4);
|
||||
dataSource.setMaxWait(100);
|
||||
dataSource.setMinIdle(0);
|
||||
dataSource.setInitialSize(1);
|
||||
dataSource.init();
|
||||
}
|
||||
|
||||
public void testConnectTimeout() throws Exception {
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
dataSource.shrink();
|
||||
Assert.assertEquals(0, dataSource.getPoolingCount());
|
||||
}
|
||||
|
||||
final List<Connection> connections = new ArrayList<Connection>();
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
Connection conn = dataSource.getConnection();
|
||||
connections.add(conn);
|
||||
}
|
||||
|
||||
final int THREAD_COUNT = 10;
|
||||
final CountDownLatch latch = new CountDownLatch(THREAD_COUNT);
|
||||
for (int i = 0; i < THREAD_COUNT; ++i) {
|
||||
Thread thread = new Thread() {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
Connection conn = dataSource.getConnection();
|
||||
Thread.sleep(1);
|
||||
conn.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
latch.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
thread.start();
|
||||
}
|
||||
|
||||
latch.await();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user