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:
@@ -0,0 +1,14 @@
|
||||
package com.alibaba.druid.pool;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DataSourceNotAvailableException extends SQLException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public DataSourceNotAvailableException(Throwable cause){
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -244,6 +244,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
|
||||
private boolean asyncCloseConnectionEnable = false;
|
||||
protected int maxCreateTaskCount = 3;
|
||||
protected boolean failFast = false;
|
||||
protected ScheduledExecutorService destroyScheduler;
|
||||
protected ScheduledExecutorService createScheduler;
|
||||
|
||||
@@ -1454,16 +1455,10 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
|
||||
createError = null;
|
||||
} catch (SQLException ex) {
|
||||
createErrorCount.incrementAndGet();
|
||||
createError = ex;
|
||||
lastCreateError = ex;
|
||||
lastCreateErrorTimeMillis = System.currentTimeMillis();
|
||||
setCreateError(ex);
|
||||
throw ex;
|
||||
} catch (RuntimeException ex) {
|
||||
createErrorCount.incrementAndGet();
|
||||
createError = ex;
|
||||
lastCreateError = ex;
|
||||
lastCreateErrorTimeMillis = System.currentTimeMillis();
|
||||
setCreateError(ex);
|
||||
throw ex;
|
||||
} catch (Error ex) {
|
||||
createErrorCount.incrementAndGet();
|
||||
@@ -1476,6 +1471,19 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
return new PhysicalConnectionInfo(conn, connectStartNanos, connectedNanos, initedNanos, validatedNanos);
|
||||
}
|
||||
|
||||
protected void setCreateError(Throwable ex) {
|
||||
createErrorCount.incrementAndGet();
|
||||
long now = System.currentTimeMillis();
|
||||
lock.lock();
|
||||
try {
|
||||
createError = ex;
|
||||
lastCreateError = ex;
|
||||
lastCreateErrorTimeMillis = now;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void initPhysicalConnection(Connection conn) throws SQLException {
|
||||
if (conn.getAutoCommit() != defaultAutoCommit) {
|
||||
conn.setAutoCommit(defaultAutoCommit);
|
||||
@@ -1758,7 +1766,15 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
this.maxCreateTaskCount = maxCreateTaskCount;
|
||||
}
|
||||
|
||||
static class PhysicalConnectionInfo {
|
||||
public boolean isFailFast() {
|
||||
return failFast;
|
||||
}
|
||||
|
||||
public void setFailFast(boolean failFast) {
|
||||
this.failFast = failFast;
|
||||
}
|
||||
|
||||
public static class PhysicalConnectionInfo {
|
||||
private Connection connection;
|
||||
private long connectStartNanos;
|
||||
private long connectedNanos;
|
||||
|
||||
@@ -277,6 +277,12 @@ public class DruidDataSource extends DruidAbstractDataSource
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
Boolean value = getBoolean(properties, "druid.failFast");
|
||||
if (value != null) {
|
||||
this.setFailFast(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isUseGlobalDataSourceStat() {
|
||||
@@ -1450,6 +1456,10 @@ public class DruidDataSource extends DruidAbstractDataSource
|
||||
DruidConnectionHolder takeLast() throws InterruptedException, SQLException {
|
||||
try {
|
||||
while (poolingCount == 0) {
|
||||
if (failFast && createError != null) {
|
||||
throw new DataSourceNotAvailableException(createError);
|
||||
}
|
||||
|
||||
emptySignal(); // send signal to CreateThread create connection
|
||||
notEmptyWaitThreadCount++;
|
||||
if (notEmptyWaitThreadCount > notEmptyWaitThreadPeak) {
|
||||
@@ -1461,7 +1471,7 @@ public class DruidDataSource extends DruidAbstractDataSource
|
||||
notEmptyWaitThreadCount--;
|
||||
}
|
||||
notEmptyWaitCount++;
|
||||
|
||||
|
||||
if (!enable) {
|
||||
connectErrorCount.incrementAndGet();
|
||||
throw new DataSourceDisableException();
|
||||
@@ -1485,6 +1495,10 @@ public class DruidDataSource extends DruidAbstractDataSource
|
||||
|
||||
for (;;) {
|
||||
if (poolingCount == 0) {
|
||||
if (failFast && createError != null) {
|
||||
throw new DataSourceNotAvailableException(createError);
|
||||
}
|
||||
|
||||
emptySignal(); // send signal to CreateThread create connection
|
||||
|
||||
if (estimate <= 0) {
|
||||
@@ -1810,6 +1824,15 @@ public class DruidDataSource extends DruidAbstractDataSource
|
||||
LOG.error("create connection error, url: " + jdbcUrl, e);
|
||||
|
||||
errorCount++;
|
||||
|
||||
if (failFast) {
|
||||
lock.lock();
|
||||
try {
|
||||
notEmpty.signalAll();
|
||||
} finally {
|
||||
lock.lock();
|
||||
}
|
||||
}
|
||||
|
||||
if (errorCount > connectionErrorRetryAttempts && timeBetweenConnectErrorMillis > 0) {
|
||||
if (breakAfterAcquireFailure) {
|
||||
@@ -1897,6 +1920,15 @@ public class DruidDataSource extends DruidAbstractDataSource
|
||||
LOG.error("create connection error, url: " + jdbcUrl, e);
|
||||
|
||||
errorCount++;
|
||||
|
||||
if (failFast) {
|
||||
lock.lock();
|
||||
try {
|
||||
notEmpty.signalAll();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
if (errorCount > connectionErrorRetryAttempts && timeBetweenConnectErrorMillis > 0) {
|
||||
if (breakAfterAcquireFailure) {
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.alibaba.druid.bvt.pool.profile;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class FailFastTest extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
private CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource() {
|
||||
public PhysicalConnectionInfo createPhysicalConnection() throws SQLException {
|
||||
try {
|
||||
latch.await();
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
SQLException ex = new SQLException();
|
||||
setCreateError(ex);
|
||||
throw ex;
|
||||
}
|
||||
};
|
||||
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setMaxWait(1000 * 1000);
|
||||
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("druid.failFast", "true");
|
||||
dataSource.configFromPropety(properties);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void testDefault() throws Exception {
|
||||
Assert.assertTrue(dataSource.isFailFast());
|
||||
|
||||
final CountDownLatch connectStartLatch = new CountDownLatch(1);
|
||||
final CountDownLatch connectEndLatch = new CountDownLatch(1);
|
||||
Thread connectThread = new Thread() {
|
||||
public void run() {
|
||||
connectStartLatch.countDown();
|
||||
try {
|
||||
dataSource.getConnection();
|
||||
} catch (SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
connectEndLatch.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
connectThread.setName("ConnectThread");
|
||||
connectThread.start();
|
||||
|
||||
connectStartLatch.await();
|
||||
|
||||
latch.countDown();
|
||||
|
||||
connectEndLatch.await(3, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user