最近在用s2sh做一个小项目。
其中。hibernate中的一对多,多对1级查询出现延时加载的问题。
然后我配置了spring的openSessionInViewFilter。
然后新增不会出现问题。但是更新出现问题。
org.springframework.orm.hibernate3.HibernateSystemException: Illegal attempt to associate a collection with two open sessions; nested exception is org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679)这个应该是缓存问题。没有好的解决方案。就改用了merge()方法。
但是做批量删除的时候
String hql="from Roles where id in(";
for(int i=0;i<roles.length-1;i++)
{
hql+=roles[i]+",";
}
hql+=roles[roles.length-1]+")";
this.getHibernateTemplate().clear();
this.getHibernateTemplate().flush();
List<Roles> list=this.getHibernateTemplate().find(hql);
this.getHibernateTemplate().deleteAll(list);
还是挂掉了。错误和上面一样。web。xml
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param> <param-name>singleSession</param-name> <param-value>false</param-value> </init-param>
</filter> <filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

解决方案 »

  1.   

    Illegal attempt to associate a collection with two open sessions 
    非法试图把一个集合用于两次开着的session 
    有可能你自己开了个session.然后spring配置又默认给这个类启动了一个session.<bean id="insertFamerAction" class="com.farmer.action.InsertFamerAction" scope="prototype">
        <property name="businessService">
          <ref bean="businessService"/>
        </property>
     </bean>hibernate中,是不允许出现同一主键对象有两个不同session同时关联的情况,如果出现这种情况hibernate会抛出"org.springframework.orm.hibernate3.HibernateSystemException: Illegal attempt to associate a collection with two open sessions; nested exception is org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions"这个异常,这个时候怎么办呢?我们需要通过hibernate提供的merge()这个方法来解决这一问题。方法原型:
    public Object merge(Object object)
    使用merge方法是有如下情况
    1、如果session中并不存在于参数object具有相同主见的对象,那么,首先需要根据主键从数据库中加载出该对象,然后将参数object中的属性拷贝到加载出来的那个对象上面,最后返回该持久化对象,同时需要注意,参数object并没有关联到session上。 2、如果session中存在与参数objcet具有相同主键属性的对象,那么直接将参数object中的属性拷贝到该持久化对象上,然后将该持久化对象返回,同时需要注意,参数object并没有关联到session上。
      

  2.   


    这种情况像是你的硬件配置不够,或者删除的数据量过大了才挂掉的吧。。如果你的数据量有几十上百万甚至千万的话,你那么写的程序,缓存累积,不停地执行N次,迟早会都会挂的对于大数据的处理: 
    可以利用线程的方式,在执行某个操作时,,不要同时去增删查啊用流控的思想做一个控制,,比如
    1. 批量执行1000-3000条记录后,线程sleep 1秒(线程方式)
    2. 每次批量处理1000-3000条记录然后进行一次提交,flash刷空session缓存
    一直循环。直到完成这样可以缓解操作大数据量的压力啊。用hibernate的批量 也可以做流控吧。用jdbc的批量操作更好
    我之前做的小的短信平台,数据量也很大,,还没挂过。