Spring管理hibernateTemplate进行写入操作报只读异常

2023-05-16

异常描述:此异常出现在web页面,在后台没有报。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.

尝试的解决方法一:按照网上说法,大多都是认为事务配置出现问题,这是从字面理解异常,也非常合理。在事务管理出把写入的方法设置为read-only=false。经过实践,还是报错。

 <!-- Transation -->
    <tx:advice id="txadvice" transaction-manager="transationManager">
        <tx:attributes>
            <tx:method name="save" read-only="false"/>
            <tx:method name="add*" read-only="false"/>
            <tx:method name="update*" read-only="false"/>
            <tx:method name="delete*" read-only="false"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

尝试的解决方法二:同样是从事务下手,在web.xml加入一个过滤器org.springframework.orm.hibernate5.support.OpenSessionInViewFilter,设置参数flushMode=AUTO/COMMIT。经过实践,我的问题还没解决。

<filter>
 
     <filter-name>OpenSessionInViewFilter</filter-name>
 
      <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
       <init-param> 
           <param-name>flushMode</param-name> 
 
           <param-value>COMMIT</param-value> 
       </init-param>
	</filter>
	<filter-mapping>
     <filter-name>OpenSessionInViewFilter</filter-name>
     <url-pattern>/*</url-pattern>
	</filter-mapping>

最后我这种情况的正确解决方法:在spring配置文件配置,hibernateTemplate bean里面配置一个属性checkWriteOperations为false。至此,问题得到解决。

 <!-- HibernateTemplate -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
        <property name="checkWriteOperations"><value>false</value></property>
    </bean>

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring管理hibernateTemplate进行写入操作报只读异常 的相关文章

随机推荐