Connection is read-only. Queries leading to data modification are not allowed

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
云数据库 RDS PostgreSQL,高可用系列 2核4GB
简介: 看了下mysql-connector-5.1.40版本中,如果设置failoverReadOnly=true (即默认值,参考链接),当mysql连接failover时,会根据jdbc连接串将当前连接的readOnly值设置为true (第8行代码) 1 2 3 4 ...
看了下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

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
12月前
|
SQL 监控 druid
springboot-druid数据源的配置方式及配置后台监控-自定义和导入stater(推荐-简单方便使用)两种方式配置druid数据源
这篇文章介绍了如何在Spring Boot项目中配置和监控Druid数据源,包括自定义配置和使用Spring Boot Starter两种方法。
|
前端开发
File和MultipartFile互相转化工具类
File和MultipartFile互相转化工具类
2089 0
|
SQL 存储 关系型数据库
解析MySQL Binlog:从零开始的入门指南【binlog入门指南】
解析MySQL Binlog:从零开始的入门指南【binlog入门指南】
12758 0
|
11月前
|
存储 安全 Java
|
SQL 关系型数据库 Java
PostgreSQL 通信协议
我们在使用数据库服务时,通常需要使用客户端连接数据库服务端,以 PostgreSQL 为例,常用的客户端有自带的 psql,JAVA 应用的数据库驱动 JDBC,可视化工具 PgAdmin 等,这些客户端都需要遵守 PostgreSQL 的通信协议才能与之 "交流"。所谓协议,可以理解为一套信息交互规则或者规范,最为我们熟知的莫过于 TCP/IP 协议和 HTTP 协议。 ![image.p
5840 0
PostgreSQL 通信协议
|
SQL druid Java
解决 ‘The last packet successfully received from the server was xxx milliseconds ago‘ 问题
解决 ‘The last packet successfully received from the server was xxx milliseconds ago‘ 问题
6136 0
|
传感器 中间件 Windows
04 Windows下mosquitto安装
04 Windows下mosquitto安装
418 0
|
JSON 前端开发 JavaScript
前端Ajax、Axios和Fetch的用法和区别笔记
前端Ajax、Axios和Fetch的用法和区别笔记
266 2
|
监控 druid Java
Springboot用JUnit测试接口时报错Failed to determine a suitable driver class configure a DataSource: ‘url‘
Springboot用JUnit测试接口时报错Failed to determine a suitable driver class configure a DataSource: ‘url‘
375 0
|
Java Maven
idea编码GBK的不可映射字符Maven修改编码格式的多种方式
idea编码GBK的不可映射字符Maven修改编码格式的多种方式
716 1

热门文章

最新文章