git add DruidDataSource async init support.

This commit is contained in:
wenshao
2017-09-28 17:12:36 +08:00
parent 7a99e4a633
commit a24df56b5a
2 changed files with 50 additions and 1 deletions
@@ -186,6 +186,14 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
configFromPropety(System.getProperties());
}
public boolean isAsyncInit() {
return asyncInit;
}
public void setAsyncInit(boolean asyncInit) {
this.asyncInit = asyncInit;
}
public void configFromPropety(Properties properties) {
{
String property = properties.getProperty("druid.name");
@@ -818,6 +826,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
SQLException connectError = null;
boolean asyncInit = this.asyncInit && createScheduler == null;
if (!asyncInit) {
try {
// init connections
@@ -2211,7 +2220,14 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
try {
boolean emptyWait = true;
if (createError != null && poolingCount == 0 && !discardChanged) {
if (createError != null
&& poolingCount == 0
&& !discardChanged) {
emptyWait = false;
}
if (emptyWait
&& asyncInit && createCount.get() < initialSize) {
emptyWait = false;
}
@@ -0,0 +1,33 @@
package com.alibaba.druid.bvt.pool;
import com.alibaba.druid.pool.DruidDataSource;
import junit.framework.TestCase;
public class AsyncInitTest extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setAsyncInit(true);
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setInitialSize(10);
dataSource.setMaxActive(10);
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_async_init() throws Exception {
dataSource.init();
for (int i = 0; i < 1000; ++i) {
if (dataSource.getPoolingCount() == dataSource.getInitialSize()) {
break;
}
Thread.sleep(10);
}
assertEquals(10, dataSource.getPoolingCount());
assertEquals(10, dataSource.getCreateCount());
}
}