JFinal多个数据源的问题

太清 发布于 2016/06/03 13:19
阅读 475
收藏 0

@JFinal 你好,想跟你请教个问题:

不知道是不是我版本太低的问题..因为项目上线了就没升版..用的是Jfinal2.0版本

现在问题是这样的..

我配置了多个数据源,当其中一个数据源出问题的时候,就直接报错了..错误都捕获不了,

另,附上我加载多数据源的方法,希望也能给大家参考

init.properties中配置多数据源


db.configs=postgresql_local
postgresql_local.db.driver=org.postgresql.Driver
postgresql_local.db.url=jdbc:postgresql://localhost:5432/wmmestest
postgresql_local.db.user=postgres
postgresql_local.db.password=postgres
postgresql_local.db.initialSize=20
postgresql_local.db.minIdle=20
postgresql_local.db.maxActive = 300
postgresql_local.db.timeBetweenEvictionRunsMillis = 60000
postgresql_local.db.maxOpenPreparedStatements=20
#See IKit.setAutoBindModel()
postgresql_local.db.autoBindModel.regex=classpath*:com/zcqm/**/model,classpath*:com/zcqm/ss/**/model
postgresql_local.db.tablePrefix=

启动JfinalConfig里

private void configDBPlugin(Plugins plugins) {
		/*************** DB CONFIG BEGIN ********************/
		// Multi DB Config From init
		// Set Config DB
		IDb.DBS = getProperty("db.configs").split(",");
		for (int i = 0; i < IDb.DBS.length; i++) {
			String dbConfig = IDb.DBS[i];
			try {
				IDataSourceProvider dataSourcePlugin;
				// DruidPlugin not support sqlite
				if (dbConfig.startsWith("sqlite3")) {
					dataSourcePlugin = new C3p0Plugin(getProperty(dbConfig + ".db.url"), getProperty(dbConfig + ".db.user"), getProperty(dbConfig + ".db.password"),
							getProperty(dbConfig + ".db.driver"));
					((C3p0Plugin) dataSourcePlugin).setInitialPoolSize(getPropertyToInt(dbConfig + ".db.initialSize", 10));
					((C3p0Plugin) dataSourcePlugin).setMinPoolSize(getPropertyToInt(dbConfig + ".db.minIdle", 10));
					((C3p0Plugin) dataSourcePlugin).setMaxIdleTime(getPropertyToInt(dbConfig + ".db.maxActive", 200));
					((C3p0Plugin) dataSourcePlugin).setAcquireIncrement(5);
				} else {
					dataSourcePlugin = new DruidPlugin(getProperty(dbConfig + ".db.url"), getProperty(dbConfig + ".db.user"), getProperty(dbConfig + ".db.password"),
							getProperty(dbConfig + ".db.driver"), "stat,wall");
					((DruidPlugin) dataSourcePlugin).set(getPropertyToInt(dbConfig + ".db.initialSize", 10), getPropertyToInt(dbConfig + ".db.minIdle", 10),
							getPropertyToInt(dbConfig + ".db.maxActive", 200));
					((DruidPlugin) dataSourcePlugin).setTimeBetweenEvictionRunsMillis(getPropertyToInt(dbConfig + ".db.timeBetweenEvictionRunsMillis", 60000));
					((DruidPlugin) dataSourcePlugin).setMaxPoolPreparedStatementPerConnectionSize(getPropertyToInt(dbConfig + ".db.maxOpenPreparedStatements", 10));
				}

				/**
				 * style if auto_bind ,only suport SimpleNameStyles.LOWER_UNDERLINE & ParamNameStyles.lowerUnderlineModule(str)
				 * SimpleNameStyles.LOWER_UNDERLINE -> javaFile: DevInfo.java bind tableName: dev_info
				 * ParamNameStyles.lowerUnderlineModule(str) -> javaFile: DevInfo.java bind tableName: str_dev_info
				 */
				INameStyle style = SimpleNameStyles.LOWER_UNDERLINE;
				if (!IUtils.isEmpty(getProperty(dbConfig + ".db.tablePrefix"))) style = ParamNameStyles.lowerUnderlineModule(getProperty(dbConfig + ".db.tablePrefix"));

				/**
				 * AutoTableBindPlugin Can Support Annotation @TableName(tableName,pkName,dbConfigName) <br />
				 */
				AutoTableBindPlugin atbp = new AutoTableBindPlugin(dbConfig, dataSourcePlugin, style);

				// 开发模式跟随系统
				atbp.setDevMode(getPropertyToBoolean("sys.devMode", false));
				// 显示SQL使用自定义的SQL显示,基本的方法不再显示SQL
				// atbp.setShowSql(true);
				// SqlReporter.setLogger(true);

				/**
				 * If dbConfig.db.autoBindModel.regex is null,then autoScan is false,also means it's the same of ActiveRecordPlugin
				 */
				boolean autoScan = false;
				if (!IUtils.isEmpty(getProperty(dbConfig + ".db.autoBindModel.regex"))) {
					autoScan = true;
					IKit.setAutoBindModel(atbp, getProperty(dbConfig + ".db.autoBindModel.regex"));
				}
				atbp.autoScan(autoScan);
				if (dbConfig.startsWith("mysql")) atbp.setDialect(new MysqlDialect());
				else if (dbConfig.startsWith("postgresql")) atbp.setDialect(new PostgreSqlDialect());
				else if (dbConfig.startsWith("oracle")) {
					atbp.setDialect(new OracleDialect());
					((DruidPlugin) dataSourcePlugin).setValidationQuery("SELECT 1 FROM DUAL");
				} else if (dbConfig.startsWith("sqlite3")) atbp.setDialect(new Sqlite3Dialect());
				else atbp.setDialect(new AnsiSqlDialect());
				atbp.setContainerFactory(new CaseInsensitiveContainerFactory(true));// 忽略大小写工厂类
				plugins.add((IPlugin) dataSourcePlugin);
				plugins.add(atbp);
				log.info("Database plugin:" + dbConfig + " init Successful.");
			} catch (Exception e) {
				log.error("数据库Config:[" + dbConfig + "]连接失败,请检查配置.", e);
				if (i == 0) throw new RuntimeException(e); // 只有0(主数据源才抛出错误,其他不抛出),这里无法捕获
			}
		}
		/*************** DB CONFIG END ********************/
	}



加载中
0
太清

这个我可以回答你...通过单插件启动 去部署..不要在配置文件中一次性加载

返回顶部
顶部