InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MA

org.springframework.dao.InvalidDataAccessApiUsageException: 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.
    at org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1175)
    at org.springframework.orm.hibernate3.HibernateTemplate$16.doInHibernate(HibernateTemplate.java:739)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:737)

 

问题来源:在Service层一个方法中调用多个dao层方法后,debug时发现调用其中2个dao层的查询方法没问题,调用一个update保存方法后报

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.错误。

 出问题的service代码如下:

public boolean getConfirmOffline(int standard_offlinetime,String searchreport_id,String wip_entity_name,OfflineControlBean bean){
		//报表传入报表名,下线标准时间,工单号,返回状态
		String offlinetime = this.offlineControlDao.getOfflinetime(searchreport_id, wip_entity_name);  --一次查询dao层方法
		SimpleDateFormat matter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		Calendar calendar = Calendar.getInstance();
		if ("".equals(offlinetime) || offlinetime == null) {
			offlinetime = matter.format(calendar.getTime());
		}
		
			Date now = new Date();
			Date before;
			try {
				before = matter.parse(offlinetime);
				long diff = now.getTime() - before.getTime();
				long hours = diff/(1000* 60 * 60);
				//当检测相差小于给定小时时,返回成功
				if( hours < standard_offlinetime){
					return true;
				}else{
					String is_lock = this.offlineControlDao.getLockflagByWipname(bean.getReport_id(), wip_entity_name);
 --一次查询dao层方法
					if(("N").equals(is_lock)){
						return true;
					}else if(("none").equals(is_lock)){
						//保存锁定记录
						bean.setFact_offlinetime(hours+"");
						bean.setCreatedate(now.toString());
						this.offlineControlDao.saveOrUpdate(bean);
 --一次更新保存对象的dao层方法
						return false;
					}
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		return false;
	}

还以为问题出在spring配置中,结果发现spring全局配置

在service层中的配置

发现spring中配置了service层关于save,add等方法开头事务的注入,那么就确定dao层saveorUpdate()这个以save为开头的命名方法没问题,spring会找到该方法并注入事务的,那么问题就出在service层了

  我们发现 service层的该方法命名为 getConfirmOffline ,而在配置中 get开头的事务,是readOnly的!!!!,发现了什么吗?就是我service层的这个方法内所有数据库操作只能是只读!!!!

       问题得解,我们只需将service层的该方法改变命名,如:saveConfirmOffline,再测试一下,发现保存对象的dao层方法成功执行了!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值