mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
DruidDataSource support failFast
This commit is contained in:
@@ -1453,7 +1453,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
validateConnection(conn);
|
||||
validatedNanos = System.nanoTime();
|
||||
|
||||
createError = null;
|
||||
setCreateError(null);
|
||||
} catch (SQLException ex) {
|
||||
setCreateError(ex);
|
||||
throw ex;
|
||||
@@ -1472,6 +1472,16 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
}
|
||||
|
||||
protected void setCreateError(Throwable ex) {
|
||||
if (ex == null) {
|
||||
lock.lock();
|
||||
try {
|
||||
createError = null;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
createErrorCount.incrementAndGet();
|
||||
long now = System.currentTimeMillis();
|
||||
lock.lock();
|
||||
|
||||
@@ -1454,6 +1454,10 @@ public class DruidDataSource extends DruidAbstractDataSource
|
||||
}
|
||||
|
||||
DruidConnectionHolder takeLast() throws InterruptedException, SQLException {
|
||||
if (failFast && createError != null) {
|
||||
throw new DataSourceNotAvailableException(createError);
|
||||
}
|
||||
|
||||
try {
|
||||
while (poolingCount == 0) {
|
||||
if (failFast && createError != null) {
|
||||
@@ -1494,11 +1498,11 @@ public class DruidDataSource extends DruidAbstractDataSource
|
||||
long estimate = nanos;
|
||||
|
||||
for (;;) {
|
||||
if (failFast && createError != null) {
|
||||
throw new DataSourceNotAvailableException(createError);
|
||||
}
|
||||
|
||||
if (poolingCount == 0) {
|
||||
if (failFast && createError != null) {
|
||||
throw new DataSourceNotAvailableException(createError);
|
||||
}
|
||||
|
||||
emptySignal(); // send signal to CreateThread create connection
|
||||
|
||||
if (estimate <= 0) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
@@ -48,6 +49,7 @@ public class FailFastTest extends TestCase {
|
||||
public void testDefault() throws Exception {
|
||||
Assert.assertTrue(dataSource.isFailFast());
|
||||
|
||||
final AtomicReference<SQLException> errorHolder = new AtomicReference<SQLException>(null);
|
||||
final CountDownLatch connectStartLatch = new CountDownLatch(1);
|
||||
final CountDownLatch connectEndLatch = new CountDownLatch(1);
|
||||
Thread connectThread = new Thread() {
|
||||
@@ -56,8 +58,7 @@ public class FailFastTest extends TestCase {
|
||||
try {
|
||||
dataSource.getConnection();
|
||||
} catch (SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
errorHolder.set(e);
|
||||
} finally {
|
||||
connectEndLatch.countDown();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user