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

问题分析:

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

 1  <!-- 事务管理 属性 -->
 2     <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
 3         <tx:attributes>
 4             <tx:method name="add*" propagation="REQUIRED"/>
 5             <tx:method name="append*" propagation="REQUIRED"/>
 6             <tx:method name="save*" propagation="REQUIRED"/>
 7             <tx:method name="update*" propagation="REQUIRED"/>
 8             <tx:method name="modify*" propagation="REQUIRED"/>
 9             <tx:method name="edit*" propagation="REQUIRED"/>
10             <tx:method name="insert*" propagation="REQUIRED"/>
11             <tx:method name="delete*" propagation="REQUIRED"/>
12             <tx:method name="remove*" propagation="REQUIRED"/>
13             <tx:method name="repair" propagation="REQUIRED"/>
14             <tx:method name="reset*" propagation="REQUIRED"/>
15 
16         
17             <tx:method name="*" propagation="REQUIRED" read-only="true"/>
18         </tx:attributes>
19     </tx:advice>
View Code

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

 1 <!-- 事务管理 属性 -->
 2     <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
 3         <tx:attributes>
 4             <tx:method name="add*" propagation="REQUIRED"/>
 5             <tx:method name="append*" propagation="REQUIRED"/>
 6             <tx:method name="save*" propagation="REQUIRED"/>
 7             <tx:method name="update*" propagation="REQUIRED"/>
 8             <tx:method name="modify*" propagation="REQUIRED"/>
 9             <tx:method name="edit*" propagation="REQUIRED"/>
10             <tx:method name="insert*" propagation="REQUIRED"/>
11             <tx:method name="delete*" propagation="REQUIRED"/>
12             <tx:method name="remove*" propagation="REQUIRED"/>
13             <tx:method name="repair" propagation="REQUIRED"/>
14             <tx:method name="reset*" propagation="REQUIRED"/>
15 
16             <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
17             <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
18             <tx:method name="load*" propagation="REQUIRED" read-only="true"/>
19             <tx:method name="search*" propagation="REQUIRED" read-only="true"/>
20             <tx:method name="datagrid*" propagation="REQUIRED" read-only="true"/>
21             <tx:method name="cancel*" propagation="REQUIRED" read-only="false"/>
22             <tx:method name="renewalOrder" propagation="REQUIRED" read-only="false"/>
23 
24             <tx:method name="*" propagation="REQUIRED" read-only="true"/>
25         </tx:attributes>
26     </tx:advice>
View Code

        其中红色如下的部分是我的方法中新增的一些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的方法:
 1 /**
 2      * 申请续租
 3      *
 4      * @param orderId
 5      * @return Object
 6      */
 7     @Transactional
 8     @Override
 9     public Object renewalOrder(String orderId) {
10 
11         Order order = new Order();
12         order.setOrderId(orderId);
13 
14         int count = orderMapper.cancelRenewal(order);
15 
16         if (count > 0) {
17             json.put("code",DataResult.RENEWAL_SUCCESS_CODE.getCode());
18             json.put("msg",DataResult.RENEWAL_SUCCESS_CODE.getMessage());
19         } else {
20             json.put("code",DataResult.FAIL_RENEWAL.getCode());
21             json.put("msg",DataResult.FAIL_RENEWAL.getMessage());
22         }
23 
24 
25         return json;
26     }
View Code

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

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



原文地址:https://www.cnblogs.com/liuyangfirst/p/8964934.html