在jfinal项目中使用定时任务quartz如果自行得到 DataSource 或 Connection ,后台会一直抛异常:
com.jfinal.plugin.activerecord.ActiveRecordException: com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed at Tue Sep 19 18:00:27 CST 2017
at com.jfinal.plugin.activerecord.DbPro.find(DbPro.java:315)
at com.jfinal.plugin.activerecord.DbPro.find(DbPro.java:326)
at com.jfinal.plugin.activerecord.Db.find(Db.java:233)
at com.sxjlrj.sys.model.ExpertMailModel.getMail(ExpertMailModel.java:22)
at com.sxjlrj.timmer.SendMsgForPersonTimmer.execute(SendMsgForPersonTimmer.java:39)
at org.quartz.core.JobRunShell.run(JobRunShell.java:206)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:548)
Caused by: com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed at Tue Sep 19 18:00:27 CST 2017
at com.alibaba.druid.pool.DruidDataSource.getConnectionInternal(DruidDataSource.java:676)
at com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:603)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:587)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:577)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:78)
at com.jfinal.plugin.activerecord.Config.getConnection(Config.java:161)
at com.jfinal.plugin.activerecord.DbPro.find(DbPro.java:312)
... 6 more
原因是:因为在定时器中手动获取了 DataSource,然后使用后关闭了或者用完后长期不再用被连接池自动关闭
解决方法是:jfinal中使用定时任务,操作数据库时,尽量使用Db+Record模式进行操作
例如:
//更新邮件记录表的邮件发送时间 和状态
Record email = Db.findById("DT_EXPERT_MAIL", msg.get("ID"));
email.set("SEND_DATE", Timestamp.valueOf(DateUtil.getNow()));
email.set("STATUS", 1);
Db.update("DT_EXPERT_MAIL", email);
另外,config配置文件添加对druid插件的操作:
//数据库连接
DruidPlugin druidPlugin = new DruidPlugin(getProperty("url"), getProperty("user"), getProperty("password").trim(),getProperty("driverClass"));
druidPlugin.setMinIdle(5)
.setTimeBetweenEvictionRunsMillis(6*1000)
//.setValidationQuery("select 1")
.setTestWhileIdle(true)
.setTestOnBorrow(true)
.setTestOnReturn(true)
.setMaxPoolPreparedStatementPerConnectionSize(20);
me.add(druidPlugin);
这样后台就不会抛异常了。