关于OpenSessionInViewFilter的配置

本文介绍如何在使用Spring和Hibernate框架时,解决由于OpenSessionInViewFilter配置不当导致的读写操作冲突问题。通过调整filter参数,确保事务正确提交。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在web.xml文件中配置OpenSessionInViewFilter

    
<filter>
        
<filter-name>hibernateFilter</filter-name>
        
<filter-class>
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        
</filter-class>
    </filter>
在spring的事务配置中:
<aop:config>
    
<aop:advisor id="managerTx" advice-ref="txAdvice"
        pointcut
="execution(* *..service.*Manager.*(..))" order="2" />
</aop:config>

<tx:advice id="txAdvice">
    
<tx:attributes>
        
<tx:method name="get*" read-only="true" />
        
<tx:method name="find*" read-only="true" />
        
<tx:method name="*" />
    
</tx:attributes>
</tx:advice>

这样的配置,当在容器中通过action调用service代码保存对象时,不能成功保存对象,
同时出现如下类似错误:
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.

这时修改filter的配置,增加如下代码:
    <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>false</param-value>
        
</init-param>
    
</filter>

这样就能成功保存对象了。

===================================================================
额。。。上面那个配置等于不用OpenSessionInViewFilter。。。 没意义。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值