Detailed interpretation: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed, Connection is read-only when Java reports an error.

problem analysis:

             In the actual development project, when inserting, this problem is a security permission protection method of the Spring framework. For the protection of things called method calls, the general configuration is as follows:

 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

           This protection mechanism is mainly because the name of the implementation method of your service is different from the original configuration. When the transaction is rolled back (rollback), your method cannot be recognized. I don’t know whether it should be rolled back or not, and you All you need to do is to let the framework know that your method needs to be rolled back like the original method, or is not needed. The settings are modified as follows

 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

        The red part below is some new service method names in my method:

<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"/> is the method of my 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

   Under the declaration, this method of yours is fine.

 

 

 

I hope that the little discovery will be helpful to you. If you find it useful, please share or like it. You can also leave a message below for discussion.

 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324990431&siteId=291194637