看了下mysql-connector-5.1.40版本中,如果设置failoverReadOnly=true (即默认值,参考链接),当mysql连接failover时,会根据jdbc连接串将当前连接的readOnly值设置为true (第8行代码)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private synchronized void switchCurrentConnectionTo(int hostIndex, MySQLConnection connection) throws SQLException {
    invalidateCurrentConnection();
 
    boolean readOnly;
    if (isPrimaryHostIndex(hostIndex)) {
        readOnly = this.explicitlyReadOnly == null false this.explicitlyReadOnly;
    else if (this.failoverReadOnly) {
        readOnly = true;
    else if (this.explicitlyReadOnly != null) {
        readOnly = this.explicitlyReadOnly;
    else if (this.currentConnection != null) {
        readOnly = this.currentConnection.isReadOnly();
    else {
        readOnly = false;
    }
    syncSessionState(this.currentConnection, connection, readOnly);
    this.currentConnection = connection;
    this.currentHostIndex = hostIndex;
}
  当执行update操作时,如果检测到readonly,就会跑出createSQLException (第8-9行代码)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
protected int executeUpdate(byte[][] batchedParameterStrings, InputStream[] batchedParameterStreams, boolean[] batchedIsStream, int[] batchedStreamLengths,
        boolean[] batchedIsNull, boolean isReallyBatch) throws SQLException {
 
    synchronized (checkClosed().getConnectionMutex()) {
 
        MySQLConnection locallyScopedConn = this.connection;
 
        if (locallyScopedConn.isReadOnly()) {
            throw SQLError.createSQLException(Messages.getString("PreparedStatement.34") + Messages.getString("PreparedStatement.35"),
                    SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
        }
 
        ....
    }
}

  上述报错信息是PreparedStatement.34和PreparedStatement.35,查mysql connector的LocalizedErrorMessages.properties

1
2
PreparedStatement.34=Connection is read-only.
PreparedStatement.35=Queries leading to data modification are not allowed

  报错信息一致。

因此, 如果是使用的主备mysql,需要手动切换master和slave,如果使用的是多主的mysql(例如,phxsql),需要设置failoverReadOnly=false

Connection is read-only. Queries leading to data modification are not allowed的更多相关文章

  1. java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

    org.springframework.dao.TransientDataAccessResourceException: ### Error updating database. Cause: ja ...

  2. 执行update操作的话,就会报“Connection is read-only. Queries leading to data modification are not allowed”的异常。

    我用的是 spring + springmvc + mybatis +mysql. <tx:advice id="txAdvice" transaction-manager= ...

  3. 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 描述:框架注入时候,配置了事物管理,权限设置 ...

  4. [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 ...

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

    问题分析: 实际开发项目中,进行insert的时候,产生这个问题是Spring框架的一个安全权限保护方法,对于方法调用的事物保护,一般配置如下: <!-- 事务管理 属性 --> < ...

  6. Connection is read-only. Queries leading to data modification are not allowed 错误原因

    因为我再spring 中使用了AOP进行事务管理,有如下配置 <tx:advice id="txAdvice" transaction-manager="trans ...

  7. 执行新增和修改操作报错connection is read-only. Queries leading to data modification are not allowed

    出现这个问题的原因是默认事务只有只读权限,因此要添加下面的每一个add*,del*,update*等等. 分别给予访问数据库的权限. 方法名的前缀有该关键字设置了read-only=true,将其改为 ...

  8. Chapter Data Modification & Chapter Data Validation

    Chapter Data Modification XF的数据提交,支持单行.集合和多层次的master-details结构的数据. Create 当提交如下数据 <Job> <Id ...

  9. Spring事务报Connection is read-only

    昨天做项目时,写了个方法,程序突然报了Connection is readonly. Queries leading to data modification are not allowed调了程序半 ...

随机推荐

  1. java web项目为什么我们要放弃jsp?

    前戏: 以前的项目大多数都是java程序猿又当爹又当妈,又搞前端(ajax/jquery/js/html/css等等),又搞后端(java/mysql/Oracle等等). 随着时代的发展,渐渐的许多 ...

  2. thinkphp错误提示:系统发生错误

    下载最新版本3.1.3,定义了一个应用,进入应用的config.php,在里面添加数据库类链接信息,在控制器里面M()一个表,访问控制器方法提示:系统发生错误.如果使用连接字符串DSN方式,调用M() ...

  3. python小知识-__call__和类装饰器的结合使用,数据描述符__get__\__set__\__delete__(描述符类是Python中一种用于储存类属性值的对象)

    class Decorator(): def __init__(self, f): print('run in init......') self.f = f def __call__(self, a ...

  4. docker swarm学习命令

    引用自:https://blog.csdn.net/wanglei_storage/article/details/77508620 引用自:https://www.cnblogs.com/wj563 ...

  5. window.jQuery || document...

    window是浏览器端的全部数据变量的引用.比如 window.window === window window.jQuery 就是浏览器中的全局变量里的jQuery那为什么不写 jQuery 而是写 ...

  6. ubuntu18.04 lts重装VMware Tools实现主机文件共享等功能

    ubuntu18.04 lts重装VMware Tools实现主机文件共享等功能 在VMWare 14.x上安装ubunuu18.04 lts后发现,可以实现全屏显示,但是没有与主机共享文件的功能,然 ...

  7. go语言中goroute使用:=遇到的坑

    先看下源代码,预想从1至N总取出所有能被a或b整除的正整数之和,为了利用go语言的并行优势,特使用goroute特性来实现,同时使用普通顺序计算进行效率比较分析 package chango impo ...

  8. macOS下通过docker在局域网成功访问mysql5.6数据库

    1.获取mysql镜像 docker pull mysql:5.6 注意:此处之所以获取mysql5.6是因为mysql5.7在centos7中启动可能会报错, 2.查看镜像列表 docker ima ...

  9. 我们为什么要学习 Spring Boot

    现在貌似大家都知道 Spring Boot 很火了,做 Java 的不知道 Spring Boot 的都已经 Out 了,但是又有多少人是跟风学习的呢?今天我们就来说一下为什么要学习 Spring B ...

  10. 爬虫三Scrapy

    Scrapy,Python开发的一个快速,高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘.监测和自动化测试. Scrapy吸引人 ...