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

 

转载于:https://www.cnblogs.com/sknn/p/7837946.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值