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

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

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


@Override
	public void recordOperLog(UserInfo user, String op_object,
			String op_action, String op_value)  throws Exception {
		
		OperlogInfo operLog = new OperlogInfo();
		// 先取出DIC
		DictionaryInfo 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");

		// log
		operLog.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值