Merged
!14 fix: 修复多数据库注入失败的问题

文涛:devNutz:dev

文涛 Created on: 2018-09-12 22:27
  1. 新增了createManyDataSource,getManySlaveDataSource来处理多数据库的注入
  2. 提取了一个公共方法来获取从库
  3. 修改了DynaDataSourceSeletor的close释放本身的DataSource
  4. 修改了depose,释放所有DataSource
  5. 测试已通过

2 comments, 2 participants 4674_wendal

Show action logs Hide action logs
Wendal merged Pull Request 2018-09-12 22:36
4674_wendal
Wendal 2018-09-12 22:31 owner

要不,先把redis那个修改排除/反向操作一下?

文涛 2018-09-12 22:33

redis的我已经删掉了呀,现在变更的文件只有3个,是要把那个提交也去掉吗

Sign in and comment

@@ -124,7 +124,7 @@ public class MainLauncher {
* [haoqoo](https://github.com/haoqoo)(提交velocity)
* [鱼夫](https://gitee.com/yustory)(正在踩NB+U家三剑客的坑)
* [幸福的旁边](https://github.com/happyday517)(提交caffeine方法缓存)
-* [文涛](https://github.com/swtseaman) (新增支持加载外部配置文件)
+* [文涛](https://gitee.com/wentao0291) (新增支持加载外部配置文件,新增多数据库连接支持)
* [zjSniper](https://gitee.com/zjSniper) (优化starter-tio的逻辑)
* [tasdingoo](https://github.com/tasdingoo)(issue@github 122)
* [csl_slchia](https://gitee.com/csl_slchia)(issue@gitee II92L)
nutzboot-starter/nutzboot-starter-jdbc/src/main/java/org/nutz/boot/starter/jdbc/DataSourceStarter.java
@@ -111,6 +111,22 @@ public class DataSourceStarter {
throw new RuntimeException("not supported jdbc.type=" + conf.get("jdbc.type"));
}
 
+ public static DataSource createManyDataSource(Ioc ioc, PropertiesProxy conf, String prefix) {
+ try {
+ String key = prefix + "type";
+ if (!conf.has(key)) {
+ throw new RuntimeException(key + " is null");
+ }
+ String type = conf.get(key);
+ if (!type.equals("druid")) {
+ throw new RuntimeException(key + "is not druid");
+ }
+ return createDruidDataSource(conf, prefix);
+ } catch (Exception e) {
+ throw new RuntimeException("datasource init error");
+ }
+ }
+
public static DataSource createSlaveDataSource(Ioc ioc, PropertiesProxy conf, String prefix) throws Exception {
switch (conf.get(prefix + "type", "druid")) {
case "simple":
@@ -165,34 +181,46 @@ public class DataSourceStarter {
if (ioc.has("slaveDataSource")) {
return ioc.get(DataSource.class, "slaveDataSource");
} else {
- // 看看有多少从数据库被定义了
- List<DataSource> slaveDataSources = new ArrayList<>();
- for (String key : conf.keys()) {
- if (key.startsWith(prefix) && key.endsWith(".url")) {
- String slaveName = key.substring(prefix.length(), key.length() - ".url".length());
- log.debug("found Slave DataSource name=" + slaveName);
- try {
- DataSource slaveDataSource = DataSourceStarter.createSlaveDataSource(ioc, conf, prefix + slaveName + ".");
- slaveDataSources.add(slaveDataSource);
- slaves.add(slaveDataSource);
- }
- catch (Exception e) {
- throw new RuntimeException(e);
- }
+ return _getSlaveDataSource(ioc, conf, prefix);
+ }
+ }
+
+ public static DataSource getManySlaveDataSource(Ioc ioc, PropertiesProxy conf, String prefix) {
+ if (ioc.has(prefix + "slaveDataSource")) {
+ return ioc.get(DataSource.class, prefix + "slaveDataSource");
+ } else {
+ return _getSlaveDataSource(ioc, conf, prefix);
+ }
+ }
+
+ private static DataSource _getSlaveDataSource(Ioc ioc, PropertiesProxy conf, String prefix) {
+ // 看看有多少从数据库被定义了
+ List<DataSource> slaveDataSources = new ArrayList<>();
+ for (String key : conf.keys()) {
+ if (key.startsWith(prefix) && key.endsWith(".url")) {
+ String slaveName = key.substring(prefix.length(), key.length() - ".url".length());
+ log.debug("found Slave DataSource name=" + slaveName);
+ try {
+ DataSource slaveDataSource = DataSourceStarter.createSlaveDataSource(ioc, conf, prefix + slaveName + ".");
+ slaveDataSources.add(slaveDataSource);
+ slaves.add(slaveDataSource);
}
- }
- // 如果的确定义了从数据库集合
- if (slaveDataSources.size() > 0) {
- if (slaveDataSources.size() == 1) {
- // 单个? 那就直接set吧
- return slaveDataSources.get(0);
- } else {
- // 多个从数据源,使用DynaDataSource进行随机挑选
- // TODO 更多更精细的挑选策略(轮训/随机/可用性...)
- return new DynaDataSource(new DynaDataSourceSeletor(slaveDataSources));
+ catch (Exception e) {
+ throw new RuntimeException(e);
}
}
}
+ // 如果的确定义了从数据库集合
+ if (slaveDataSources.size() > 0) {
+ if (slaveDataSources.size() == 1) {
+ // 单个? 那就直接set吧
+ return slaveDataSources.get(0);
+ } else {
+ // 多个从数据源,使用DynaDataSource进行随机挑选
+ // TODO 更多更精细的挑选策略(轮训/随机/可用性...)
+ return new DynaDataSource(new DynaDataSourceSeletor(slaveDataSources));
+ }
+ }
return null;
}
 
@@ -213,7 +241,7 @@ public class DataSourceStarter {
}
 
public void close() throws IOException {
- for (DataSource dataSource : slaves) {
+ for (DataSource dataSource : ds) {
try {
if (dataSource instanceof Closeable)
((Closeable) dataSource).close();
nutzboot-starter/nutzboot-starter-nutz-dao/src/main/java/org/nutz/boot/starter/nutz/dao/NutDaoStarter.java
@@ -200,12 +200,12 @@ public class NutDaoStarter {
// 获取数据库名称
String name = match.group(1);
String prefix_name = "jdbc.many." + name + ".";
- DataSource manyDataSource = DataSourceStarter.createDataSource(ioc, conf, prefix_name);
+ DataSource manyDataSource = DataSourceStarter.createManyDataSource(ioc, conf, prefix_name);
NutDao nutDao = new NutDao();
nutDao.setDataSource(manyDataSource);
// 处理对应的从库
String slave_prefix = prefix_name + "slave.";
- DataSource slaveDataSource = DataSourceStarter.getSlaveDataSource(ioc, conf, slave_prefix);
+ DataSource slaveDataSource = DataSourceStarter.getManySlaveDataSource(ioc, conf, slave_prefix);
if(slaveDataSource != null) {
NutDaoRunner runner = new NutDaoRunner();
runner.setSlaveDataSource(slaveDataSource);