今天学习了下HighAvailableDataSource的内容,从名字不难看出它是一个可以提供高可用能力的数据源,其官网解释如下:
DataSource class which contains multiple DataSource objects.
下面从getConnection方法开始,看看它与DruidDataSource有何区别:
@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();
}
从上面的方法可以看出它和DruidDataSource不同的是,HighAvailableDataSource的getConnection方法是从一个selector对象中get一个DataSource对象并获取连接返回。那么这个selector又是个什么东西呢?
selector其实是一个DataSourceSelector对象,在HighAvailableDataSource执行init方法的时候会执行setSelector方法初始化:
public void setSelector(String name) {
DataSourceSelector selector = DataSourceSelectorFactory.getSelector(name, this);
if (selector != null) {
selector.init();
setDataSourceSelector(selector);
}
}