开发者社区> 问答> 正文

JFinal Druid DataSourceClosedException?报错

JFinal 1.8, druid 1.0.4

我有一个TimerTask每隔30分从数据库清除过期的session数据,结果报错

Exception in thread "ExpiresSessionService" com.jfinal.plugin.activerecord.ActiveRecordException: com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed at Wed Jun 04 13:22:05 CST 2014
	at com.jfinal.plugin.activerecord.DbPro.update(DbPro.java:279)
	at com.jfinal.plugin.activerecord.DbPro.update(DbPro.java:290)
	at com.jfinal.plugin.activerecord.Db.update(Db.java:214)
	at com.power.oj.core.model.SessionModel.expiresSession(SessionModel.java:48)
	at com.power.oj.core.service.SessionService.expiresSession(SessionService.java:139)
	at com.power.oj.service.ExpiresSessionService.run(ExpiresSessionService.java:22)
	at java.util.TimerThread.mainLoop(Timer.java:555)
	at java.util.TimerThread.run(Timer.java:505)
Caused by: com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed at Wed Jun 04 13:22:05 CST 2014
	at com.alibaba.druid.pool.DruidDataSource.getConnectionInternal(DruidDataSource.java:990)
	at com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:898)
	at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:4544)
	at com.alibaba.druid.filter.FilterAdapter.dataSource_getConnection(FilterAdapter.java:2724)
	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:880)
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:872)
	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:97)
	at com.jfinal.plugin.activerecord.Config.getConnection(Config.java:185)
	at com.jfinal.plugin.activerecord.DbPro.update(DbPro.java:276)
	... 7 more 

代码:

public class ExpiresSessionService extends TimerTask
{

  private final static Logger log = Logger.getLogger(ExpiresSessionService.class);
  private static boolean start = false;
  private static ExpiresSessionService daemon;
  private static Timer click_timer;
  private final static long INTERVAL = 30 * 60 * 1000;

  @Override
  public void run()
  {
    int numOfExpiresSession = SessionService.me().expiresSession();
    log.info(new StringBuilder(3).append("ExpiresSessionService executed: ").append(numOfExpiresSession).append(" sessions deleted.").toString());
  }

  public static void start()
  {
    if (!start)
    {
      daemon = new ExpiresSessionService();
      click_timer = new Timer("ExpiresSessionService", true);
      click_timer.schedule(daemon, INTERVAL, INTERVAL);
      start = true;
      log.info("ExpiresSessionService started.");
    }
  }

}



public int expiresSession()
  {
    return Db.update("DELETE FROM session WHERE sessionExpires <= UNIX_TIMESTAMP()");
  }





展开
收起
爱吃鱼的程序员 2020-06-20 17:21:13 2603 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    <spanstyle="font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;line-height:14.666666030883789px;background-color:#FFFFFF;">SessionService.me().expiresSession();单例?回复<aclass='referer'target='_blank'>@魔神翼:谢谢!用quartz多线程调度处理数据岂不太悲剧了,哎回复<aclass='referer'target='_blank'>@littleant:参考JFinal的回答,多个线程只有一个数据库连接导致被自动关闭。应该在每次运行任务时,重新建立连接。回复<aclass='referer'target='_blank'>@魔神翼:请问,这个和单例有啥直接关系?回复<aclass='referer'target='_blank'>@魔神翼:解决就好还真是单例模式的问题可能是拿到手的Connection长时间不用,被Druid自动关闭回复<aclass="referer"target="_blank">@JFinal:使用model.find查询数据库,放在quartz定时任务中,会出现datasourcealreadyclosed回复<aclass='referer'target='_blank'>@魔神翼:建议现用现拿比较好,否则长时间占用connection不好难道没有重新连接机制么

    2020-06-23 13:16:50
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Java Spring Boot开发实战系列课程【第15讲】:Spring Boot 2.0 API与Spring REST Docs实战 立即下载
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题) 立即下载
Java Spring Boot开发实战系列课程【第7讲】:Spring Boot 2.0安全机制与MVC身份验证实战(Java面试题) 立即下载