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()"); }
<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不好难道没有重新连接机制么
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。