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层方法成功执行了!!!