作者微信 bishe2022

代码功能演示视频在页面下方,请先观看;如需定制开发,联系页面右侧客服
java操作数据报Connection is read-only错误

Custom Tab

后台controller层调用插入函数:

@RequestMapping("/saveGoods.htm")
	@ResponseBody
    public Result saveGoods(ModelMap map, HttpServletRequest request) 
    {
		String strGoodsList = request.getParameter("goodsList");
		List<Goods> lstGoods = JSON.parseArray(strGoodsList, Goods.class);
		Result result = new Result();
		int nCount = goodsService.insertBatchGoods(lstGoods);
    	return result;
    }


后台打印日志为:

; SQL []; Connection is read-only. Queries leading to data modification are not allowed; nested exception is java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
16398 [http-bio-8080-exec-9] DEBUG org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver  - Resolving exception from handler [com.main.controller.GoodsController@7813124d]: org.springframework.dao.TransientDataAccessResourceException: 
### Error updating database.  Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
### The error may involve com.main.dao.GoodsMapper.insertBatchGoods-Inline
### The error occurred while setting parameters
### SQL: insert into t_goods (GoodsNo, GoodsName, GoodsNum, GoodsPrice)        values                    (?,?,?,?)        ,            (?,?,?,?)        ,            (?,?,?,?)        ,            (?,?,?,?)
### Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed


查看spring的事务配置, 发现service事务控制只进行了add*, update*,delete*,del*拦截操作,其它操作都是只读的;


<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<!-- 必须要rollback-for才能事务回滚 -->
			<tx:method name="add*"    propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
			<tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
			<tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
			<tx:method name="del*"    propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
			<!-- <tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/> -->
			<tx:method name="*"       read-only="true" />
		</tx:attributes>
	</tx:advice>

添加上下面的配置,运行正常,数据也能插入

<tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>


Home