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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,高可用系列 2核4GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 看了下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互相转化工具类
2133 0
|
SQL 存储 关系型数据库
解析MySQL Binlog:从零开始的入门指南【binlog入门指南】
解析MySQL Binlog:从零开始的入门指南【binlog入门指南】
13113 0
|
Java Windows
【问题总结】【JAVA开发】(一)Intellj JVM启动报错
一)启动前提,最新社区版intellj 默认支持1.9 以上。将默认jdk20 替换成jdk8 出现以下问题 Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. Unrecognized option: --add-opens
907 0
|
1月前
|
设计模式 人工智能 缓存
2025架构革命:一文深度揭秘AI四维进化(MoE/GraphRAG/智能体/HyDE)
本文深入解析大模型核心技术与实践原理,涵盖MCP、RAG、Agent、微调等关键技术,结合架构演进与实战技巧,助你构建高性能AI系统,建议点赞收藏。
439 0
|
11月前
|
存储 安全 Java
|
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‘ 问题
6412 0
|
SQL 关系型数据库 Java
PostgreSQL 通信协议
我们在使用数据库服务时,通常需要使用客户端连接数据库服务端,以 PostgreSQL 为例,常用的客户端有自带的 psql,JAVA 应用的数据库驱动 JDBC,可视化工具 PgAdmin 等,这些客户端都需要遵守 PostgreSQL 的通信协议才能与之 "交流"。所谓协议,可以理解为一套信息交互规则或者规范,最为我们熟知的莫过于 TCP/IP 协议和 HTTP 协议。 ![image.p
5862 0
PostgreSQL 通信协议
|
消息中间件 监控 安全
Kafka客户端工具:Offset Explorer 使用指南
Kafka客户端工具:Offset Explorer 使用指南
8016 0
|
Java
FreeMarker - 四种变量的用法
FreeMarker - 四种变量的用法
589 1

热门文章

最新文章