关于@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优先级大于注解,如果我说错了,请大佬指正。