详细解读 :java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed,Java报错之Connection is read-only.

时间:2021-02-14 09:35:45

问题分析:

实际开发项目中,进行insert的时候,产生这个问题是Spring框架的一个安全权限保护方法,对于方法调用的事物保护,一般配置如下:

  <!-- 事务管理 属性 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="append*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="edit*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="repair" propagation="REQUIRED"/>
<tx:method name="reset*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>

这个保护机制主要是你的service的实现方法命名跟这个原始的配置的有差别,事务处理回滚(rollback)的时候对你的方法无法识别,不知道是应该回滚还是不回滚,而你需要做的就是让框架知道你的方法,是跟原来的方法一样需要回滚的,或者是不需要的,设置修改如下

 <!-- 事务管理 属性 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="append*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="edit*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="repair" propagation="REQUIRED"/>
<tx:method name="reset*" propagation="REQUIRED"/> <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="load*" propagation="REQUIRED" read-only="true"/>
<tx:method name="search*" propagation="REQUIRED" read-only="true"/>
<tx:method name="datagrid*" propagation="REQUIRED" read-only="true"/>
<tx:method name="cancel*" propagation="REQUIRED" read-only="false"/>
<tx:method name="renewalOrder" propagation="REQUIRED" read-only="false"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>

其中红色如下的部分是我的方法中新增的一些service方法命名:

<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="load*" propagation="REQUIRED" read-only="true"/>
<tx:method name="search*" propagation="REQUIRED" read-only="true"/>
<tx:method name="datagrid*" propagation="REQUIRED" read-only="true"/>
<tx:method name="cancel*" propagation="REQUIRED" read-only="false"/>
<tx:method name="renewalOrder" propagation="REQUIRED" read-only="false"/>
最后这个
<tx:method name="renewalOrder" propagation="REQUIRED" read-only="false"/> 是我的serviceImpl的方法:
 /**
* 申请续租
*
* @param orderId
* @return Object
*/
@Transactional
@Override
public Object renewalOrder(String orderId) { Order order = new Order();
order.setOrderId(orderId); int count = orderMapper.cancelRenewal(order); if (count > 0) {
json.put("code",DataResult.RENEWAL_SUCCESS_CODE.getCode());
json.put("msg",DataResult.RENEWAL_SUCCESS_CODE.getMessage());
} else {
json.put("code",DataResult.FAIL_RENEWAL.getCode());
json.put("msg",DataResult.FAIL_RENEWAL.getMessage());
} return json;
}

声明下,你的这个方法就可以了.

希望小小发现,对您有所帮助,如果觉得有用请分享或点赞,也可以在下面留言,大家共同讨论