项目是之前程序员写的,我接手不久,有些东西看不太懂,麻烦各位解答一下!
项目初始配置了数据源
- @Override
- public void configPlugin(Plugins me) {
- // 基础核心数据库 onlyone
- DruidPlugin druidPlugin = new DruidPlugin(ConfigPropKit.get("jdbc.url"),
- ConfigPropKit.get("jdbc.username"),
- ConfigPropKit.get("jdbc.password"),
- ConfigPropKit.get("jdbc.driverClassName"));
- druidPlugin.set(10, 100, 1000);
- druidPlugin.addFilter(new StatFilter());
- druidPlugin.addFilter(new WallFilter());
- druidPlugin.setLogAbandoned(true);
- druidPlugin.setRemoveAbandoned(true);
- druidPlugin.setValidationQuery("SELECT 1");
- druidPlugin.setTestOnBorrow(true);
- me.add(druidPlugin);
- // 基础自动绑定model与表关系插件
- AutoTableBindPlugin autoTableBindPlugin = new AutoTableBindPlugin(druidPlugin, SimpleNameStyles.LOWER);
- autoTableBindPlugin.setCache(new MyCache());
- autoTableBindPlugin.setShowSql(ConfigPropKit.getToBool("jdbc.showsql", true));
- autoTableBindPlugin.setContainerFactory(new CaseInsensitiveContainerFactory());
- autoTableBindPlugin.setDialect(new MysqlDialect()); //使用AnsiSqlDialect
- autoTableBindPlugin.addJars("bmnsp-model-1.0.jar");
- autoTableBindPlugin.addScanPackages("org.bmnsp.model");
- me.add(autoTableBindPlugin);
- // 新增 配置文件插件
- me.add(new ConfigPlugin());
- // 缓存插件
- me.add(new EhCachePlugin());
- // 新增 quarz定时任务插件
- me.add(new QuartzPlugin("job.properties"));
- if(JFinalKit.getConstants().getDevMode()) {
- logger.debug("--------插件配置完毕!!!");
- }
- }
紧接着下面又第二个数据源的配置
- @Override
- public void afterJFinalStart() {
- // 清空缓存
- CacheKit.getCacheManager().clearAll();
- // 缓存配置
- config();
- // 配置token 集群实现
- TokenManager.init(new TokenCache("session", "tokenCache"));
- if(JFinalKit.getConstants().getDevMode()) {
- logger.debug("--------启动完毕!!!");
- }
- // 添加备份数据源
- DruidPlugin druidPlugin1 = new DruidPlugin(
- ConfigPropKit.get("jdbc2.url"),
- ConfigPropKit.get("jdbc2.username"),
- ConfigPropKit.get("jdbc2.password"),
- ConfigPropKit.get("jdbc2.driverClassName"));
- druidPlugin1.set(10, 100, 5000);
- druidPlugin1.addFilter(new StatFilter());
- druidPlugin1.start();
- DbKit.addConfig(new Config(Consts.GBSDB, druidPlugin1.getDataSource(), new MysqlDialect()));
- Sqlconfig.URL=ConfigPropKit.get("jdbc.url");
- Sqlconfig.USERNAME=ConfigPropKit.get("jdbc.username");
- Sqlconfig.PASSWORD=ConfigPropKit.get("jdbc.password");
- }
在这个里“Consts.GBSDB”已经定义在Consts类
- public static final String GBSDB = "gps_db";
在控制器进行以下调用时就会报错
- List<Record> list = Db.use(Consts.GBSDB).find("SELECT en.card_id, en.card_x, en.card_y, en.card_z, en.time FROM tb_locationdata en JOIN ( SELECT card_id, max(id) mxid FROM tb_locationdata GROUP BY card_id ORDER BY id DESC ) tmp ON en.card_id = tmp.card_id AND en.id = tmp.mxid where en.time > DATE_SUB(NOW(),INTERVAL 5 second);");
报错如下
- java.lang.IllegalArgumentException: Config not found by configName: gps_db
- at com.jfinal.plugin.activerecord.DbPro.<init>(DbPro.java:74) ~[jfinal-ry-3.0.jar:na]
- at com.jfinal.plugin.activerecord.DbPro.use(DbPro.java:81) ~[jfinal-ry-3.0.jar:na]
- at com.jfinal.plugin.activerecord.Db.use(Db.java:35) ~[jfinal-ry-3.0.jar:na]
- at org.bmnsp.web.job.OneMinutesJob.execute(OneMinutesJob.java:50) ~[OneMinutesJob.class:na]
- at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.2.1.jar:na]
- at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.1.jar:na]
- [message]: Job (org.bmnsp.web.job.OneMinutesJob.org.bmnsp.web.job.OneMinutesJob threw an exception.
- [class]: org.quartz.core.ErrorLogger
- [time]: 2020-09-11 18:01:40.025 DefaultQuartzScheduler_Worker-1 org.quartz.core.ErrorLogger.schedulerError ERROR
- org.quartz.SchedulerException: Job threw an unhandled exception.
- at org.quartz.core.JobRunShell.run(JobRunShell.java:213) ~[quartz-2.2.1.jar:na]
- at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.1.jar:na]
- Caused by: java.lang.IllegalArgumentException: Config not found by configName: gps_db
- at com.jfinal.plugin.activerecord.DbPro.<init>(DbPro.java:74) ~[jfinal-ry-3.0.jar:na]
- at com.jfinal.plugin.activerecord.DbPro.use(DbPro.java:81) ~[jfinal-ry-3.0.jar:na]
- at com.jfinal.plugin.activerecord.Db.use(Db.java:35) ~[jfinal-ry-3.0.jar:na]
- at org.bmnsp.web.job.OneMinutesJob.execute(OneMinutesJob.java:50) ~[OneMinutesJob.class:na]
- at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.2.1.jar:na]
- ... 1 common frames omitted
项目:JFinal
回到具体问题,异常关键是:Config not found by configName: gps_db
这个异常出现在你代码中的地点在:OneMinutesJob.execute(OneMinutesJob.java:50)
上面这行代码的第 50 行,使用了 Db.use("gps_db"), 而这个 gps_db 根本就不存在
那么如何让这个东东存在呢? 很简单,在 new ActiveRecordPlugin 的时候,第一个参数就是了:
ActiveRecordPlugin arp = new ActiveRecordPlugin("gps_db", 其它参数在此);
最后,一定要先稍微看下文档,节省大量时间