码迷,mamicode.com
首页 > 其他好文 > 详细

don't flush the Session after an exception occurs

时间:2015-01-13 12:34:32      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:hibernate   session   事务   

前几天项目在上生产环境的运行发生了一个“don‘t flush the Session after an exception occurs”异常,在网上查了很多资料,发现讲的都挺废柴的,因为都是根据自己的问题讲了解决方法,根本没有讲什么是“don‘t flush the Session after an exception occurs”,为什么会发生"don‘t flush the Session after an exception occurs",其实这个异常是Hibernate内部定义的一个异常,发生这个异常的原因是你使用了一个已经报错的session,也就是session已经报错了,还使用这个session。第二次使用这个session时,就会报“don‘t flush the Session after an exception occurs”。


现在分析下我为什么会报这个异常:

    @Scheduled(cron = "0 30 0 * * ? ")
    @Transactional
    public void scheduleGetVmSurveryInfo() {
        try {
            hostMonitorService.doVmSurveryInfo("LINUX", 100);
            hostMonitorService.doVmSurveryInfo("WINDOWS", 100);
        } catch (Exception e) {
            logger.error("ScheduleServiceImpl调用scheduleGetVmSurveryInfo出异常了,信息为:" + e.getMessage());
        }
    }


doVmSurveryInfo方法上也加了    @Transactional,由于我内部底层操作hql获取session的方式是getCurentSession(),故“linux”和"windows"两个操作使用了同一个session,在linux操作中,session报错了(原因我的数据库中name字段分配的字节只有20,实际数据很大,存不够,所以session报错了),等进行"windows"操作时,还是用该session,由于seesion已经报错了,所以会报"don‘t flush the Session after an exception occurs"这个错误,我这个解决方法是:给name字节分配大一点,第二:给"linux"和"windows"各自分配一个事务,这样两个就用不同的session,就不会出现这个错误。


我写这篇博客只想说明为什么会报“don‘t flush the Session after an exception occurs”错误,这个错误是什么。我的例子不用明白,我写的也不是很全。

don't flush the Session after an exception occurs

标签:hibernate   session   事务   

原文地址:http://blog.csdn.net/tlqfreedom/article/details/42674009

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!