org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read

来源:互联网 发布:磁力搜索 知乎 编辑:程序博客网 时间:2024/05/12 19:36

异常: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.


有可能是因为类中的方法没有符合spring事务配置的命名规范,或者对应的命名配置的是只读的事务,而你进行的是更新操作。


首先看spring的配置文件:

现在spring的事务机制是添加在了业务层,service层

<aop:config expose-proxy="true"><!-- 只对业务逻辑层实施事务 --><aop:pointcut id="txPointcut"expression="execution(* com.rxtmedia.base.service..*.*(..))" /><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /></aop:config>


这是配置对哪些方法进行事务管理


<tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED" /><tx:method name="add*" propagation="REQUIRED" /><tx:method name="create*" propagation="REQUIRED" /><tx:method name="insert*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="merge*" propagation="REQUIRED" /><tx:method name="del*" propagation="REQUIRED" /><tx:method name="remove*" propagation="REQUIRED" /><tx:method name="put*" propagation="REQUIRED" /><tx:method name="use*" propagation="REQUIRED" /><tx:method name="recordOperLog" propagation="REQUIRED" /><!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 --><tx:method name="get*" propagation="REQUIRED" read-only="true" /><tx:method name="count*" propagation="REQUIRED" read-only="true" /><tx:method name="find*" propagation="REQUIRED" read-only="true" /><tx:method name="list*" propagation="REQUIRED" read-only="true" /><span style="color:#ff0000;"><tx:method name="*" read-only="true" /> // 其他的命名方法都是只读事务</span></tx:attributes></tx:advice>

我在项目中,记录日志时候的方法是:


@Overridepublic void recordOperLog(UserInfo user, String op_object,String op_action, String op_value)  throws Exception {OperlogInfo operLog = new OperlogInfo();// 先取出DICDictionaryInfo dic1 = dictionaryInfoDAO.findByProperty("itemCode", op_object).get(0);DictionaryInfo dic2 = dictionaryInfoDAO.findByProperty("itemCode", op_action).get(0);SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");// logoperLog.setUserId(user);operLog.setOperUser(user);operLog.setCreateTime(sdf1.parse(sdf1.format(new Date())));operLog.setDoes(dic1);operLog.setOperEvent(dic2);operLog.setOperValue(op_value);operLog.setOperDate(sdf2.parse(sdf2.format(new Date())));operlogInfoDAO.add(operLog);}

因为在service层我的方法名是  recordOperLog(...);所以不在  读写 事务里,属于只读事务,我在这个方法最后对数据库进行插入操作是不行的。


解决方法:

1.要么在配置文件里,把你的方法的命名规范添加上去

比如在加一行 :“  <tx:method name="record*" propagation="REQUIRED" />”  类似这样的配置。

2.将你的方法改成符合 读写事务  方法的 命名规范  addOperLog();  这样的方法名称。



0 0
原创粉丝点击