From 8f313c4857467a11624b997a25b6cc9582db0b47 Mon Sep 17 00:00:00 2001 From: abel Date: Wed, 29 Aug 2018 14:32:37 +0800 Subject: [PATCH] =?UTF-8?q?BI-29723=20druid=E8=BF=9E=E6=8E=A5=E6=B1=A0?= =?UTF-8?q?=E7=94=9F=E4=BA=A7=E8=BF=9E=E6=8E=A5=E7=BA=BF=E7=A8=8B=E9=98=BB?= =?UTF-8?q?=E5=A1=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/druid/pool/DruidDataSource.java | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java b/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java index 8fd1f3937..f9a79e459 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java @@ -139,6 +139,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat private DestroyTask destroyTask; private CreateConnectionThread createConnectionThread; + private PeriodDetectionThread periodDetectionThread; private DestroyConnectionThread destroyConnectionThread; private LogStatsThread logStatsThread; private int createTaskCount; @@ -613,10 +614,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } private void checkThread() throws SQLException { - if (createConnectionThread == null){ + if (createConnectionThread == null) { throw new IllegalStateException("createConnectionThread not start!"); } - if (destroyConnectionThread == null){ + if (destroyConnectionThread == null) { throw new IllegalStateException("destroyConnectionThread not start!"); } if (!createConnectionThread.isStarted() && !destroyConnectionThread.isStarted()) { @@ -780,7 +781,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat createAndLogThread(); createAndStartCreatorThread(); createAndStartDestroyThread(); - + createAndStartDetectThread(); initedLatch.await(); init = true; @@ -859,6 +860,14 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat initedLatch.countDown(); } + private void createAndStartDetectThread() { + if(createScheduler == null){ + String threadName = "Druid-ConnectionPool-Detection-" + System.identityHashCode(this) + this.getUrl(); + periodDetectionThread = new PeriodDetectionThread(threadName); + periodDetectionThread.start(); + } + } + /** * load filters from SPI ServiceLoader @@ -1526,7 +1535,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat if (destroyConnectionThread != null) { destroyConnectionThread.interrupt(); } - + if (periodDetectionThread != null){ + periodDetectionThread.interrupt(); + } if (destroySchedulerFuture != null) { destroySchedulerFuture.cancel(true); } @@ -2198,6 +2209,33 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat this.started = started; } } + //周期性检查生产线程状态,因为在终止生产线程的时候,为了不让生产线程疯狂重试数据库,只是生成了一个生产线程,但是并没有start,需要一个守护线程 + //周期性检查线程状态,帮助其启动。 + private class PeriodDetectionThread extends Thread { + public PeriodDetectionThread(String name) { + super(name); + this.setDaemon(true); + } + + public void run() { + while(true) { + synchronized (DruidDataSource.this) { + //生产线程发生了切换,并且有线程在等待连接,需要主动唤醒生产线程,否则由 + if (!createConnectionThread.started && !destroyConnectionThread.started && notEmptyWaitThreadCount > 0) { + createConnectionThread.setStarted(true); + createConnectionThread.start(); + destroyConnectionThread.setStarted(true); + destroyConnectionThread.start(); + } + } + try { + Thread.sleep(timeBetweenConnectErrorMillis); + } catch (InterruptedException ignore) { + break; + } + } + } + } public class DestroyConnectionThread extends Thread { private volatile boolean started = true;