详细解读 :java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed,Java报错之Connection is read-only.
问题分析:
实际开发项目中,进行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;
}
声明下,你的这个方法就可以了.
希望小小发现,对您有所帮助,如果觉得有用请分享或点赞,也可以在下面留言,大家共同讨论
详细解读 :java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed,Java报错之Connection is read-only.的更多相关文章
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
org.springframework.dao.TransientDataAccessResourceException: ### Error updating database. Cause: ja ...
- [Done]java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed ...
- java最全的Connection is read-only. Queries leading to data modification are not allowed
Connection is read-only. Queries leading to data modification are not allowed 描述:框架注入时候,配置了事物管理,权限设置 ...
- Connection is read-only. Queries leading to data modification are not allowed
看了下mysql-connector-5.1.40版本中,如果设置failoverReadOnly=true (即默认值,参考链接),当mysql连接failover时,会根据jdbc连接串将当前连接 ...
- 执行update操作的话,就会报“Connection is read-only. Queries leading to data modification are not allowed”的异常。
我用的是 spring + springmvc + mybatis +mysql. <tx:advice id="txAdvice" transaction-manager= ...
- Connection is read-only. Queries leading to data modification are not allowed 错误原因
因为我再spring 中使用了AOP进行事务管理,有如下配置 <tx:advice id="txAdvice" transaction-manager="trans ...
- 执行新增和修改操作报错connection is read-only. Queries leading to data modification are not allowed
出现这个问题的原因是默认事务只有只读权限,因此要添加下面的每一个add*,del*,update*等等. 分别给予访问数据库的权限. 方法名的前缀有该关键字设置了read-only=true,将其改为 ...
- Mysql报错java.sql.SQLException:null,message from server:"Host '27,45,38,132' is not allowed to connect
Mysql报错java.sql.SQLException:null,message from server:"Host '27,45,38,132' is not allowed to co ...
- java.sql.SQLException: null, message from server: "Host '192.168.xxx.xxx' is not allowed to connect to this MySQL server"
当你连接自己的电脑上的MySQL时,报这样的错,你可以把ip换成 127.0.0.1或者localhost ,当然前提是用户名和密码正确
随机推荐
- day 31 表单标签,CSS
一. HTML表单标签 HTML表单用于搜集不同类型的用户输入,然后把数据提交给服务器处理. 常用的表单标签: 标签 作用 form 所有表单标签的根标签 input 输入标签,包括单行输入框.密码框 ...
- xampp配置多个监听端口和不同的网站目录
1.配置Apache文件httpd.conf 打开xampp安装目录下的Apache->conf文件夹下的httpd.conf,用记事本打开 首先在Listen 80端口下添加其他监听端口: L ...
- Java_10 继承
1 继承的好处 继承的出现提高了代码的复用性,提高软件开发效率. 继承的出现让类与类之间产生了关系,提供了多态的前提. 2 继承的注意事项 在Java中,类只支持单继承,不允许多继承,也就是说一个类只 ...
- 5-java 排序, sort, collections.sort()
https://blog.csdn.net/whp1473/article/details/79678974 import java.util.ArrayList; import java.util. ...
- git--(3)分支 合并
git branch test //新建分支 git branch //列出分支 git branch -r //列出远程分支 git branch -m | -M oldbranch newbran ...
- 关于vue的npm run dev和npm run build
├─build │ ├─build.js │ ├─check-versions.js │ ├─dev-client.js │ ├─dev-server.js │ ├─utils.js │ ├─vue- ...
- ERROR: APK path is not specified for
1. 打开project structure 2.设置outpath路径 最好为绝对路径 点击确定 重新编译即可. Note: Android Studio版本使用
- python pyMysql 自定义异常 函数重载
# encoding='utf8'# auth:yanxiatingyu#2018.7.24 import pymysql __all__ = ['Mymysql'] class MyExcept(E ...
- 5A - 超级楼梯
有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法? Input 输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1< ...
- Java VisualVM 插件地址
打开Java VisualVM检查更新插件时,默认的连接连不上,通过浏览器访问之后发现默认的服务器已经404,新地址已经迁移到github,下面这个地址里面有不同版本jdk对应的插件中心地址. htt ...