[SSH] Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction

本文介绍了解决Hibernate在使用FlushMode.NEVER时遇到的错误的方法。通过配置Spring的事务管理器,确保所有涉及数据修改的操作都在事务范围内执行,从而避免了因flush模式不当导致的问题。

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

当执行到service层时,发生了如题的错误。
原因:

OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flush mode
设为FlushMode.NEVER。然后把该sessionFactory绑定到TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该sessionFactory的绑定,最后closeSessionIfNecessary根据该session是否已和transaction绑定来决定是否关闭session。在这个过程中,若HibernateTemplate
发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO
Session,使方法拥有写权限。也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为Flush.AUTO,拥有insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flush
model的话,则doFilter的整个过程都是Flush.NEVER。所以受transaction(声明式的事务)保护的方法有写权限,没受保护的则没有。

解决办法:在spring配置文件中加上这一部分

<tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
                <tx:method name="create*" propagation="REQUIRED" />
                <tx:method name="save*" propagation="REQUIRED" />
                <tx:method name="add*" propagation="REQUIRED" />
                <tx:method name="update*" propagation="REQUIRED" />
                <tx:method name="remove*" propagation="REQUIRED" />
                <tx:method name="del*" propagation="REQUIRED" />
                <tx:method name="import*" propagation="REQUIRED" />
                <tx:method name="*" propagation="REQUIRED"/>
                <!--这里如果觉着麻烦只要最后一行*就可以了,因为他会扫描所有的函数的 -->
            </tx:attributes>
        </tx:advice>

        <aop:config proxy-target-class="true">
            <aop:pointcut id="serviceOperation" expression="execution(* com.hdu.service.impl..*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />

或者如果不想配置配置文件

,请在service层上面加上一个 @Transactional

这样service层就加入了事务列,然后在所有的dao层中的操作加一行getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);像下面这样

public void doCreateUser(Student student) {
    getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
        this.getHibernateTemplate().save(student);         

    }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值