From ea16b58cc000c114607b1fceca3d693d9dae6798 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Tue, 5 Nov 2019 18:00:48 +0800 Subject: [PATCH] Update RandomDataSourceSelector.java if threads are running, interrupt them first, and then start new threads --- .../pool/ha/selector/RandomDataSourceSelector.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java index 693b3351c5..fdf60993b4 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java @@ -50,6 +50,8 @@ public class RandomDataSourceSelector implements DataSourceSelector { private HighAvailableDataSource highAvailableDataSource; private RandomDataSourceValidateThread validateThread; private RandomDataSourceRecoverThread recoverThread; + private Thread runningValidateThread; + private Thread runningRecoverThread; private int checkingIntervalSeconds = 15; private int recoveryIntervalSeconds = 120; @@ -161,14 +163,22 @@ public class RandomDataSourceSelector implements DataSourceSelector { validateThread.setValidationSleepSeconds(validationSleepSeconds); validateThread.setBlacklistThreshold(blacklistThreshold); } - new Thread(validateThread, "RandomDataSourceSelector-validate-thread").start(); + if (runningValidateThread != null) { + runningValidateThread.interrupt(); + } + runningValidateThread = new Thread(validateThread, "RandomDataSourceSelector-validate-thread"); + runningValidateThread.start(); if (recoverThread == null) { recoverThread = new RandomDataSourceRecoverThread(this); recoverThread.setSleepSeconds(recoveryIntervalSeconds); recoverThread.setValidationSleepSeconds(validationSleepSeconds); } - new Thread(recoverThread, "RandomDataSourceSelector-recover-thread").start(); + if (runningRecoverThread != null) { + runningRecoverThread.interrupt(); + } + runningRecoverThread = new Thread(recoverThread, "RandomDataSourceSelector-recover-thread"); + runningRecoverThread.start(); } private Collection removeBlackList(Map dataSourceMap) {