關于@Transactional(readOnly = false)注解,資料新增修改Connection is read-only
有時你新增或修改會報Connection is read-only
Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
這是因為你配置了資料庫連接配接為隻讀,不能新增修改,而配置隻讀的方法有2個一個是xml裡面配置事物的時候配置方法隻讀
<!-- 事務管理 屬性 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="append*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
<tx:method name="modify*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="edit*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="del*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="*Tran" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="remove*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="repair" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="get*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="count*" 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="do*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="send*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
最後一句
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
所有不是配置裡的字首方法都為隻讀。
另一個配置隻讀就是注解@Transactional(readOnly = true),配置這個注解就會不能新增修改。
有時候@Transactional(readOnly = false)不能生效,這個可能是xml配置與注解配置有優先級的關系,我簡單測試了一下是xml優先級大于注解,如果我說錯了,請大佬指正。