diff --git a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java index d5fc9dd5b7..be1e9b69f1 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java +++ b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java @@ -72,7 +72,7 @@ public class DataSourceCreator { HighAvailableDataSource haDataSource) throws SQLException { DruidDataSource dataSource = new DruidDataSource(); - dataSource.setName(name); + dataSource.setName(name + "-" + System.identityHashCode(dataSource)); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); @@ -122,8 +122,8 @@ public class DataSourceCreator { private void loadNameList() { Set names = new HashSet(); for (String n : properties.stringPropertyNames()) { - if (n.contains(".")) { - names.add(n.split("\\.")[0]); + if (n.contains(".url")) { + names.add(n.split("\\.url")[0]); } } if (!names.isEmpty()) { diff --git a/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java b/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java index 0946c1bcc8..b347944293 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java @@ -89,14 +89,26 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc private DataSourceSelector selector = new RandomDataSourceSelector(this); private String dataSourceFile = DEFAULT_DATA_SOURCE_FILE; + private boolean inited = false; + public void init() throws SQLException { + if (inited) { + return; + } synchronized (this) { + if (inited) { + return; + } if (dataSourceMap == null || dataSourceMap.isEmpty()) { dataSourceMap = new DataSourceCreator(dataSourceFile).createMap(this); } if (selector == null) { selector = new RandomDataSourceSelector(this); } + if (dataSourceMap == null || dataSourceMap.isEmpty()) { + LOG.warn("There is NO DataSource available!!! Please check your configuration."); + } + inited = true; } } @@ -117,7 +129,12 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc @Override public Connection getConnection() throws SQLException { + init(); DataSource dataSource = selector.get(); + if (dataSource == null) { + LOG.warn("Can NOT obtain DataSource, return null."); + return null; + } return dataSource.getConnection(); } @@ -145,12 +162,20 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc } DataSourceSelector selector = DataSourceSelectorFactory.getSelector(name, this); if (selector != null) { - this.selector = selector; + setDataSourceSelector(selector); } } - public DataSourceSelector getSelector() { - return selector; + public String getSelector() { + return selector == null ? null : selector.getName(); + } + + public DataSourceSelector getDataSourceSelector() { + return this.selector; + } + + public void setDataSourceSelector(DataSourceSelector dataSourceSelector) { + this.selector = dataSourceSelector; } @Override diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/DataSourceSelector.java b/src/main/java/com/alibaba/druid/pool/ha/selector/DataSourceSelector.java index d8497e3f84..d6646a2cd2 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/DataSourceSelector.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/DataSourceSelector.java @@ -28,4 +28,5 @@ public interface DataSourceSelector { DataSource get(); void setTarget(String name); boolean isSame(String name); + String getName(); } diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/NamedDataSourceSelector.java b/src/main/java/com/alibaba/druid/pool/ha/selector/NamedDataSourceSelector.java index b53e7caa5a..11cbf293bf 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/NamedDataSourceSelector.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/NamedDataSourceSelector.java @@ -37,7 +37,12 @@ public class NamedDataSourceSelector implements DataSourceSelector { @Override public boolean isSame(String name) { - return "byName".equalsIgnoreCase(name); + return getName().equalsIgnoreCase(name); + } + + @Override + public String getName() { + return "byName"; } @Override 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 345bff84bc..f5434d391a 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 @@ -17,6 +17,8 @@ package com.alibaba.druid.pool.ha.selector; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.ha.HighAvailableDataSource; +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; import javax.sql.DataSource; import java.util.Collection; @@ -33,6 +35,8 @@ import java.util.concurrent.CopyOnWriteArrayList; * @author DigitalSonic */ public class RandomDataSourceSelector implements DataSourceSelector { + private final static Log LOG = LogFactory.getLog(RandomDataSourceSelector.class); + private Random random = new Random(); private List blacklist = new CopyOnWriteArrayList(); private HighAvailableDataSource highAvailableDataSource; @@ -41,15 +45,24 @@ public class RandomDataSourceSelector implements DataSourceSelector { public RandomDataSourceSelector(HighAvailableDataSource highAvailableDataSource) { this.highAvailableDataSource = highAvailableDataSource; - validateThread = new RandomDataSourceValidateThread(this); - recoverThread = new RandomDataSourceRecoverThread(this); - new Thread(validateThread, "RandomDataSourceSelector-validate-thread").start(); - new Thread(recoverThread, "RandomDataSourceSelector-recover-thread").start(); + if (!highAvailableDataSource.isTestOnBorrow() && !highAvailableDataSource.isTestOnReturn()) { + validateThread = new RandomDataSourceValidateThread(this); + recoverThread = new RandomDataSourceRecoverThread(this); + new Thread(validateThread, "RandomDataSourceSelector-validate-thread").start(); + new Thread(recoverThread, "RandomDataSourceSelector-recover-thread").start(); + } else { + LOG.info("testOnBorrow or testOnReturn has been set to true, ignore validateThread"); + } } @Override public boolean isSame(String name) { - return "random".equalsIgnoreCase(name); + return getName().equalsIgnoreCase(name); + } + + @Override + public String getName() { + return "random"; } @Override diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java index a1900a8634..e2887fe349 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java @@ -18,6 +18,7 @@ package com.alibaba.druid.pool.ha.selector; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.logging.Log; import com.alibaba.druid.support.logging.LogFactory; +import com.alibaba.druid.util.JdbcUtils; import javax.sql.DataSource; import java.sql.Connection; @@ -95,25 +96,28 @@ public class RandomDataSourceValidateThread implements Runnable { private boolean check(String name, DruidDataSource dataSource) { boolean result = true; Driver driver = dataSource.getRawDriver(); - Properties info = dataSource.getConnectProperties(); - String url = dataSource.getRawJdbcUrl(); + Properties info = new Properties(dataSource.getConnectProperties()); + String username = dataSource.getUsername(); + String password = dataSource.getPassword(); + String url = dataSource.getUrl(); // We can't use rawUrl here, because the schema maybe set in url. Connection conn = null; + if (info.getProperty("user") == null && username != null) { + info.setProperty("user", username); + } + if (info.getProperty("password") == null && password != null) { + info.setProperty("password", password); + } try { LOG.debug("Validating " + name + " every " + sleepSeconds + " seconds."); conn = driver.connect(url, info); dataSource.validateConnection(conn); } catch (SQLException e) { - LOG.warn("Validation FAILED for " + name + ". Exception: " + e.getMessage()); + LOG.warn("Validation FAILED for " + name + " with url [" + url + "] and username [" + + info.getProperty("user") + "]. Exception: " + e.getMessage()); result = false; } finally { - if (conn != null) { - try { - conn.close(); - } catch (SQLException e) { - LOG.error("Can not close connection for HostAndPort Validation.", e); - } - } + JdbcUtils.close(conn); } return result; diff --git a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java index 6a10653abb..a38cf40f4b 100644 --- a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java +++ b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java @@ -30,8 +30,8 @@ public class RandomDataSourceSelectorWithValidationTest { @Test public void testOneDataSourceFailAndRecover() throws Exception { - ((RandomDataSourceSelector) highAvailableDataSource.getSelector()).getValidateThread().setSleepSeconds(3); - ((RandomDataSourceSelector) highAvailableDataSource.getSelector()).getRecoverThread().setSleepSeconds(3); + ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector()).getValidateThread().setSleepSeconds(3); + ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector()).getRecoverThread().setSleepSeconds(3); Thread.sleep(30 * 1000); @@ -40,7 +40,7 @@ public class RandomDataSourceSelectorWithValidationTest { Thread.sleep(10 * 1000); assertTrue(dataSource.isTestOnReturn()); for (int i = 0; i < 100; i++) { - assertNotEquals(dataSource, highAvailableDataSource.getSelector().get()); + assertNotEquals(dataSource, highAvailableDataSource.getDataSourceSelector().get()); } dataSource.setValidationQuery(null); @@ -48,7 +48,7 @@ public class RandomDataSourceSelectorWithValidationTest { assertFalse(dataSource.isTestOnReturn()); int count = 0; for (int i = 0; i < 100; i++) { - if (dataSource == highAvailableDataSource.getSelector().get()) { + if (dataSource == highAvailableDataSource.getDataSourceSelector().get()) { count++; } } @@ -57,8 +57,8 @@ public class RandomDataSourceSelectorWithValidationTest { @Test public void testAllDataSourceFail() throws Exception { - ((RandomDataSourceSelector) highAvailableDataSource.getSelector()).getValidateThread().setSleepSeconds(3); - ((RandomDataSourceSelector) highAvailableDataSource.getSelector()).getRecoverThread().setSleepSeconds(3); + ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector()).getValidateThread().setSleepSeconds(3); + ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector()).getRecoverThread().setSleepSeconds(3); Thread.sleep(30 * 1000); @@ -73,7 +73,7 @@ public class RandomDataSourceSelectorWithValidationTest { int[] count = new int[2]; for (int i = 0; i < 100; i++) { - DataSource dataSource = highAvailableDataSource.getSelector().get(); + DataSource dataSource = highAvailableDataSource.getDataSourceSelector().get(); if (foo == dataSource) { count[0]++; } else if (bar == dataSource) {