PostgreSQL之IOException

news/2024/7/9 20:50:51 标签: postgresql, 数据库, holder is null, IOException

IOException_0">PostgreSQL之IOException

PostgreSQL 是一款开源的对象关系型数据库管理系统,在使用过程中可能会遇到 IOExcption 异常,这通常是由以下原因之一导致的:

1. Sends a 2-byte integer (short) to the back end

原因:

  1. 使用SQL语言in时超过Short.MAX_VALUE最大数量时,抛出此异常,源代码如下:
  2. 第1点产生的异常会有一个关闭数据库连接的操作,导致业务后续数据库操作拿不到连接,从而抛出另一个错误Cause: java.sql.SQLException: connection holder is null

    注:在PostgreSQL数据库驱动jar的org.postgresql.core.v3.QueryExecutorImpl类中,IOExcpetion捕获后,都有一个abort();执行,方法详细代码为:pgStream.getSocket().close();

解决方案:

// 分批处理,每次In的数量1000条
// 可以用Hutool工具类来处理
CollUtil.split(list, 2000);

org.postgresql.core.PGStream;

  public void sendInteger2(int val) throws IOException {
    if (val < Short.MIN_VALUE || val > Short.MAX_VALUE) {
      throw new IOException("Tried to send an out-of-range integer as a 2-byte value: " + val);
    }

    _int2buf[0] = (byte) (val >>> 8);
    _int2buf[1] = (byte) val;
    pg_output.write(_int2buf);
  }

org.postgresql.core.v3.QueryExecutorImpl

try {
      try {
        handler = sendQueryPreamble(handler, flags);
        autosave = sendAutomaticSavepoint(query, flags);
        sendQuery(query, (V3ParameterList) parameters, maxRows, fetchSize, flags,
            handler, null);
        if ((flags & QueryExecutor.QUERY_EXECUTE_AS_SIMPLE) != 0) {
          // Sync message is not required for 'Q' execution as 'Q' ends with ReadyForQuery message
          // on its own
        } else {
          sendSync();
        }
        processResults(handler, flags);
        estimatedReceiveBufferBytes = 0;
      } catch (PGBindException se) {
        // There are three causes of this error, an
        // invalid total Bind message length, a
        // BinaryStream that cannot provide the amount
        // of data claimed by the length arugment, and
        // a BinaryStream that throws an Exception
        // when reading.
        //
        // We simply do not send the Execute message
        // so we can just continue on as if nothing
        // has happened. Perhaps we need to
        // introduce an error here to force the
        // caller to rollback if there is a
        // transaction in progress?
        //
        sendSync();
        processResults(handler, flags);
        estimatedReceiveBufferBytes = 0;
        handler
            .handleError(new PSQLException(GT.tr("Unable to bind parameter values for statement."),
                PSQLState.INVALID_PARAMETER_VALUE, se.getIOException()));
      }
    } catch (IOException e) {
      // 关闭数据库连接
      abort();
      handler.handleError(
          new PSQLException(GT.tr("An I/O error occurred while sending to the backend."),
              PSQLState.CONNECTION_FAILURE, e));
    }
@Override
public void abort() {
  try {
    pgStream.getSocket().close();
  } catch (IOException e) {
    // ignore
  }
  closed = true;
}

总结

遇到代码异常后不要慌(其时大多数错误都是第一次碰见),首先仔细观查日志,并根据关键字查找源代码,排查错误产生的原因


http://www.niftyadmin.cn/n/5046332.html

相关文章

您的计算机已被.360勒索病毒感染?恢复您的数据的方法在这里!

导言&#xff1a; 在数字时代&#xff0c;威胁网络安全的恶意软件不断演化&#xff0c;而.360勒索病毒则是近年来备受关注的一种。这种勒索病毒以其高度复杂的加密技术和毒害性的行为而著称&#xff0c;威胁着用户的数据和隐私。本文91数据恢复将深入剖析.360勒索病毒&#xf…

2023-9-23 区间选点

题目链接&#xff1a;区间选点 #include <iostream> #include <algorithm>using namespace std;const int N 100010;int n;struct Range {int l, r;bool operator< (const Range &W) const{return r < W.r;} }range[N];int main() {scanf("%d"…

Ubuntu编辑.bashrc

1.打开文件 1.1 使用命令打开 sudo gedit ~/.bashrc sudo nano ~/.bashrc sudo vim ~/.bashrc sudo可以省略 1.2 手动打开 在主目录下找到这个文件手动打开进行编辑&#xff0c;如果隐藏了&#xff0c;使用ctrlh让他显示出来 2.编辑完、保存、退出 按下 Ctrl X 键&…

怎么在VM虚拟机中安装kali linux系统?

由于个人兴趣,最近学习了在VM中安装Kali Linux系统,在此记录方便日后查阅,同时希望也能帮助到正在看这篇文章的你。 目录 1.VM虚拟机简介 2.Kali Linux系统简介 3.下载Kali Linux系统 3.1-迅雷种子下载 3.2-压缩包下载 <

Mysql——压缩包方式安装教程

一.Mysql压缩包下载方式 zip版&#xff08;5.7及8.0&#xff09;的下载需到官方网站下载&#xff0c;不同版本对应能安装在不同的操作系统下&#xff0c;本次介绍的是mysql-8.0.30-winx64在win10下的安装方式。 下载网址&#xff1a;MySQL :: Download MySQL Community Server …

深度学习综述:Computation-efficient Deep Learning for Computer Vision: A Survey

论文作者&#xff1a;Yulin Wang,Yizeng Han,Chaofei Wang,Shiji Song,Qi Tian,Gao Huang 作者单位&#xff1a;Tsinghua University; Huawei Inc. 论文链接&#xff1a;http://arxiv.org/abs/2308.13998v1 内容简介&#xff1a; 在过去的十年中&#xff0c;深度学习模型取…

【360秋招笔试】编程题第二题:修改Web(C++的AC解法)

题目 先看样例&#xff1a; 6 1612*3 7*8*954 11122 4*6222 15712 11115n表示输入n行数据&#xff0c;下面每一行数据表示一个等式。如果能满足 在等式中添加任意一个数字 使得等式两边成立&#xff0c;则输出Yes&#xff0c;否则输出No。如果等式本来就相等&#xff0c;也输…

mysql workbench常用操作

1、No database selected Select the default DB to be used by double-clicking its name in the SCHEMAS list in the sidebar 方法一&#xff1a;双击你要使用的库 方法二&#xff1a;USE 数据库名 2、复制表名&#xff0c;字段名 3、保存链接