zoukankan      html  css  js  c++  java
  • hibernate flushMode 错误

     1 十一月 15, 2017 10:13:36 上午 org.apache.struts2.dispatcher.Dispatcher error
     2 严重: Exception occurred during processing request: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into Flu
     3 shMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
     4 org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session
     5 into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
     6         at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1128)
     7         at org.springframework.orm.hibernate4.HibernateTemplate$16.doInHibernate(HibernateTemplate.java:685)
     8         at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:341)
     9         at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:309)
    10         at org.springframework.orm.hibernate4.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:682)
    .
    .
    .

    错误描述:由于没有使用事务,当hibernate持久化数据到数据库时,控制台报出该错误,按照错误描述,是hibernate的flushMode设置不正确,需要修改.

    软件环境:Struts2-Spring Ioc-Hibernate4 DB:Oracle11g.

    解决办法:1 使用事务;2 修改flushMode;

    1 使用spring-tx 在service层中开启事务; 注意 基于注解的service层不能多次扫描 !

    spring-service.xml

      <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
      </bean>
      <tx:annotation-driven/>
    
      <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
          <tx:method name="do*" propagation="REQUIRED" rollback-for="Exception" />
          <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
          <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
          <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception" />
          <tx:method name="mod*" propagation="REQUIRED" rollback-for="Exception" />
          <tx:method name="ins*" propagation="REQUIRED" rollback-for="Exception" />
          <tx:method name="upd*" propagation="REQUIRED" rollback-for="Exception" />
          <tx:method name="invoke" propagation="REQUIRES_NEW" rollback-for="Exception" />
          <tx:method name="*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
      </tx:advice>

    service层:

    @Service("userService")
    public class UserServiceImpl implements UserService {
    
      @Autowired
      private UserDao userDao;
    
      @Transactional
      public void addUser(SUserEntity user) {
        ...
      }
      ...
    }

    问题解决.

    2 在dao层修改flushMode: 建议修改为AUTO,主要还是使用事务.

    @Repository("userDao")
    public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
    
      @Resource(name = "sessionFactory")
      public void setSessionFactoryDI(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
      /*this.getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);*/
    }
    public void saveUser(SUserEntity user) {
         Session session = null;
         Transaction tr = null;
         try {
           session = HibernateSessionFactory.getSession();
           tr = session.beginTransaction();
           session.saveOrUpdate(t);
           tr.commit();
           HibernateSessionFactory.closeSesssion();
         } catch (Exception e) {
             e.printStackTrace();
             tr.rollback();
           }
    }
    ...
    }

    参考: https://stackoverflow.com/questions/6810158/java-hibernate-write-operations-are-not-allowed-in-read-only-mode

        http://blog.csdn.net/looyo/article/details/6309136">http://blog.csdn.net/looyo/article/details/6309136

  • 相关阅读:
    2017第17周四当前工作中困境与挑战思考
    2017第17周三
    2017第17周二
    最小可行产品
    《穷查理宝典》中三条最重要的学习方法
    机场打车有感
    2017第15周五
    2017第15周四
    三条经济学原理帮你做出正确的选择
    Mac通过安装Go2Shell实现“在当前目录打开iTerm2”
  • 原文地址:https://www.cnblogs.com/sknn/p/7837946.html
Copyright © 2011-2022 走看看