<!-- lang: java -->
org.springframework.orm.hibernate3.HibernateSystemException: Illegal attempt to associate a collection with two open sessions;
错误原因分析: 因为Hibernate默认是只允许单个session存在,如果有两个session同时open,并同一个collection进行操作,Hibernate是无法判断使用那个。 在wel.xml文件中配置了opensession Filter,设置为singleSession。
<!-- lang: xml -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
但是如果我们想通过改变singleSession为false解决问题,我个人认为这个是不合理做法,还是该想其他解决方法。我提供一个解决方法供大家参考。
解决方法: 而且我通过google的搜索的时候,发现这个问题经常出现在1:N的情况下。所以在这种情况下要更为注意open two session这个问题,以免浪费必要的时间在解决错误上。
1、将两个同时对数据库操作的(update/save)法写在一个事务中。
2、将Hibernate的update方法改为merge。
本文针对Hibernate框架中因多Session并发操作同一collection导致的错误,提供了两种解决方案:一是将相关操作放入同一事务;二是将update操作替换为merge操作。
870

被折叠的 条评论
为什么被折叠?



