开发者社区> 问答> 正文

Write operations are not allowed in read-only mode (FlushMode.MANUAL)

Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

当我写数据的时候出现上面异常,查啊查,应该是保存数据时采用了默认的只读模式。

网上的方法配置事务:

<!-- 定义事务管理 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- 配置事务异常封装 -->
   <bean id="persistenceExceptionTranslationPostProcessor"
       class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    
   <!--  声明式容器事务管理 ,transaction-manager指定事务管理器为transactionManager -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="get*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
     
    <aop:config expose-proxy="true">
        <!-- 只对业务逻辑层实施事务 -->
        <aop:pointcut id="txPointcut" expression="execution(* com.zq.app.cms.service..*.*(..))" />
        <!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice -->
        <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
    </aop:config>

2.修改默认只读模式:

<filter>
      <filter-name>OpenSessionInViewFilter</filter-name>
      <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
      <init-param> -->
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>sessionFactory</param-value>
      </init-param>
      <init-param>
        <param-name>flushMode</param-name>
        <param-value>AUTO</param-value>
      </init-param>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

都没用。费解。。。

我采用的是spring 4.20  hibernate 4.3.10.Final 

public class HibernateDAO extends HibernateDaoSupport{
     
    private static final Log log = LogFactory.getLog(HibernateDAO.class);
     
    /**
     * save object 插入一条数据
     * 
     * @param obj
     */
    public void save(Object obj) {
        log.debug("save  instance");
        try {
            getHibernateTemplate().save(obj);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

求高手指点。。。

我把OpenSessionInViewFilter类覆盖了

/**
     * Open a Session for the SessionFactory that this filter uses.
     * <p>The default implementation delegates to the {@link SessionFactory#openSession}
     * method and sets the {@link Session}'s flush mode to "MANUAL".
     * @param sessionFactory the SessionFactory that this filter uses
     * @return the Session to use
     * @throws DataAccessResourceFailureException if the Session could not be created
     * @see org.hibernate.FlushMode#MANUAL
     */
    protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
        try {
            Session session = sessionFactory.openSession();
            session.setFlushMode(FlushMode.MANUAL);
            return session;
        }
        catch (HibernateException ex) {
            throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
        }
    }

把 penSession()方法中 session.setFlushMode(FlushMode.MANUAL); 改为session.setFlushMode(FlushMode.AUTO);
结果是可以的。

展开
收起
a123456678 2016-03-18 15:58:54 3003 0
1 条回答
写回答
取消 提交回答
  • 我在hibernate.cfg.xml 配置文件中去掉jta

    就解决了这个问题,具体的原理现在还不是太清楚还需要查下资料,我现在判断可能是它影响了spring对HibernateTemplate 事务的接管. 希望对你能有帮助!

    2019-07-17 19:06:55
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
AliHB Real-Time Cold data Backup 立即下载
AliHB Real Time Cold data Backup 立即下载
Clipper,A Low-latency Online 立即下载