penghuaiyi2008 2008-11-07 09:58
浏览 257
已采纳

OpenSessionInViewFilter疑问

为了解决Hibernate延迟加载问题,我在web.xml中加入了OpenSessionInViewFilter配置

<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>







我的事务配置代码:



<!---->

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<!---->
&lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
    &lt;property name="locations"&gt;
        &lt;list&gt;
            &lt;value&gt;classpath*:spring/*.properties&lt;/value&gt;
        &lt;/list&gt;
    &lt;/property&gt;
&lt;/bean&gt;

<!---->
&lt;aop:config proxy-target-class="true"&gt;
   &lt;aop:advisor pointcut="execution(* com.zx.blog.*.service.impl.*Service*.*(..))" advice-ref="txAdvice"&gt;
&lt;/aop:config&gt;

<!---->
&lt;tx:advice id="txAdvice"&gt;
    &lt;tx:attributes&gt;
        &lt;tx:method name="save*"&gt;
        &lt;tx:method name="update*"&gt;
        &lt;tx:method name="remove*"&gt;
        &lt;tx:method name="delete*"&gt;
        &lt;tx:method name="get*" read-only="true"&gt;
        &lt;tx:method name="load*" read-only="true"&gt;
        &lt;tx:method name="find*" read-only="true"&gt;
    &lt;/tx:attributes&gt;
&lt;/tx:advice&gt;

</beans>











但问题出来了,在添加数据保存的时候老是提示 org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.



数据保存不了。不知那位高手是否也遇到过此问题,有什么好的解决办法呢?



  • 写回答

3条回答 默认 最新

  • iteye_12540 2008-11-07 09:58
    关注

    很明显,你事务配置没有做好。使用了这个过滤器后,其工作是这样的,请求到过滤器后,打开Session,但是此时是只读模式,只能读,不能写。遇到Spring的带事务的Bean的时候,根据事务管理器的定义,确定是否转换为读写模式。当这个事务处理完以后,再次转回只读模式。
    从你的错误上看,就是你在只读模式下进行了写操作,所以就出错了。
    你查看一下你的Spring事务配置.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大