原作者:彭冲
- 适用范围
- 问题概述
- 问题原因
- 解决方案
适用范围
当前所有MogDB版本
问题概述
客户使用spring jdbc接口执行一条插入语句执行报错,并且连接被关闭,错误截图如下:

问题原因
根据客户提供的截图进行本地代码模拟,主要测试java代码如下:
int count = 1171;
System.out.println("一次insert插入数据行数:" + count);
Connection conn = DriverManager.getConnection(jdbcUrl,jdbcUser,jdbcPass);
StringBuffer bufferSql = new StringBuffer();
bufferSql.append("INSERT INTO test_bulk_insert ");
bufferSql.append(" SELECT test_bulk_insert_id_seq.nextval as id, foo.* FROM (");
bufferSql.append(" SELECT ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ");
for(int i=1;i<count;i++){
bufferSql.append(" UNION ALL ");
bufferSql.append(" SELECT ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ");
}
bufferSql.append(") foo");
PreparedStatement stmt = conn.prepareStatement(bufferSql.toString());
int j = 0;
for(int i=1;i<count*28+1;i++){
stmt.setString(i, "abc");
j++;
}
stmt.execute();
测试表每行使用28个绑定变量,当插入1171行时提示如下报错:
Exception in thread "main" org.postgresql.util.PSQLException: [192.168.20.1:50293/192.168.20.200:3040] socket is not closed; Urgent packet sent to backend successfully; An I/O error occured while sending to the backend.detail:Tried to send an out-of-range integer as a 2-byte value: 32788;
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:390)
at org.postgresql.jdbc.PgStatement.runQueryExecutor(PgStatement.java:562)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:539)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:397)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:156)
at org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:145)
at test.jdbc.demo.Test1SqlBulkInsert.main(Test1SqlBulkInsert.java:73)
Caused by: java.io.IOException: Tried to send an out-of-range integer as a 2-byte value: 32788
at org.postgresql.core.PGStream.sendInteger2(PGStream.java:237)
at org.postgresql.core.v3.QueryExecutorImpl.sendParse(QueryExecutorImpl.java:1649)
at org.postgresql.core.v3.QueryExecutorImpl.sendOneQuery(QueryExecutorImpl.java:2139)
at org.postgresql.core.v3.QueryExecutorImpl.sendQuery(QueryExecutorImpl.java:1444)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:354)
... 6 more
提示绑定变量的个数超过了两个字节的范围,也就是不能超过32768。
解决方案
根据jdbc对绑定变量不能超过32768的硬限制,降低插入数据的条数。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




