关于Jfinal配合quartz进行定时任务。数据源用的是Druid,经常出现数据源连接超时,网上说用Jfinal Db+Record 模式就可以解决该问题,我所有定时任务用的都是Jfinal Db+Record 模式 ,但是还是经常出现以下报错,问题并没有解决,
druid配置如下:
@Override public void configPlugin(Plugins me) { /**配置druid数据连接池插件**/ DruidPlugin dp = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"),PropKit.get("driverClass")); dp.setMaxActive(30). setInitialSize(1) .setMaxWait(60000) .setMinIdle(5).setTimeBetweenEvictionRunsMillis(6000) .setMinEvictableIdleTimeMillis(300000) .setValidationQuery("SELECT 'x'") .setTestWhileIdle(true). setTestOnBorrow(false). setTestOnReturn(false). setFilters("stat"). /**配置druid监控**/ addFilter(new StatFilter()).//sql监控 addFilter(new WallFilter()).//防止sql注入 setMaxPoolPreparedStatementPerConnectionSize(20); WallFilter wall=new WallFilter(); wall.setDbType("mysql"); dp.addFilter(wall); me.add(dp); ActiveRecordPlugin arp = MyDB.loadTableConfig(dp); // arp.setShowSql(true); me.add(arp);
定时任务代码如下:
@Scheduled(fixedDelay = 1000 * (60 * 60 * 1)) // 间隔一个小时执行 /* @Scheduled(cron="0 0 0 * * ?") */// 固定时间执行 public class SysAdopt implements Job { private static Log logger = LogFactory.getLog(SysAdopt.class); @Override public void execute(JobExecutionContext arg) throws JobExecutionException { try { // 查询所有系统选择的问题 Date beginDate = new Date(); Calendar date = Calendar.getInstance(); date.setTime(beginDate); date.set(Calendar.DATE, date.get(Calendar.DATE) - 1); String endDate = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date.getTime()); logger.info("之前的时间===" + endDate); // List<Question> questions =Question.dao.queryForList("*", "selectType = 1 and createDate<= ? and state = 0", endDate); List<Record> questions = Db.find("select * from question where selectType = 1 and createDate<= ? and state = 0", endDate), orderlist=Db.find("select paymentNo,orderNo,type from order_table where state=1 and paymentNo is not null and wxOrder is null"); logger.info("系统选择问题 定时任务测试开始==="); for (Record question : questions) { UserPriceUtil.calculatingUserAmount(question); } if(StringUtil.isNotEmpty(orderlist)){ List<OrderTable> list=new ArrayList<>(); OrderTable order; boolean useCert; for (Record o : orderlist) { useCert=o.getInt("type")==2; Map<String, String> wxOrder= PaymentApi.orderQuery(o.get(useCert?"orderNo":"paymentNo"),useCert); order=new OrderTable(); order.set("wxOrder", JSONObject.toJSONString(wxOrder)); order.set("paymentNo", o.get("paymentNo")); order.set("orderNo", o.get("orderNo")); list.add(order); } Db.batch("update order_table set wxOrder =? where paymentNo=? and orderNo=?", "wxOrder, paymentNo,orderNo", list, list.size()); } logger.info("系统选择问题 定时任务结束==="); } catch (Exception e) { logger.error("定时任务系统选择问题异常:",e); } } }
2018:04:20:15:56:17 ERROR [com.admin.util.quartz.SysAdopt] (SysAdopt.java:47) - 定时任务系统选择问题异常: com.jfinal.plugin.activerecord.ActiveRecordException: com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed at Mon Apr 16 13:59:58 CST 2018 at com.jfinal.plugin.activerecord.Model.find(Model.java:565) at com.admin.model.MyDB.find(MyDB.java:69) at com.admin.model.MyDB.queryForList(MyDB.java:82) at com.admin.util.quartz.SysAdopt.execute(SysAdopt.java:40) 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 Mon Apr 16 13:59:58 CST 2018 at com.alibaba.druid.pool.DruidDataSource.getConnectionInternal(DruidDataSource.java:1057) at com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:960) at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:4544) at com.alibaba.druid.filter.FilterAdapter.dataSource_getConnection(FilterAdapter.java:2723) at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:4540) at com.alibaba.druid.filter.stat.StatFilter.dataSource_getConnection(StatFilter.java:661) at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:4540) at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:938) at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:930) at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:102) at com.jfinal.plugin.activerecord.Config.getConnection(Config.java:161) at com.jfinal.plugin.activerecord.Model.find(Model.java:562) ... 5 more
项目:JFinal
//解决BAE3.0 mysql公有数据库30秒断连接限制
druidPlugin.setTestWhileIdle(true);
druidPlugin.setTestOnBorrow(true);
druidPlugin.setTestOnReturn(true);拿去调试下。