java.io.IOException: Tried to send an out-of-range integer as a 2-byte value

本文讨论了在使用 PostgreSQL JDBC 驱动时,预设语句参数数量受限于客户端的 2 字节整数限制的问题。通过将参数列表分割成每批 50,000 个参数,可以有效解决此问题,避免出现超出参数限制的异常。

The newest feature in the software I’m developing at Just Software is a workstream, which is our aggregate name for microblog and activity stream functionality. Since we’re using a relational DB we’ve modeled this module in the way that every workstream message is attached to a user’s stream in a simple m:n relation manner. To reduce the number of INSERT statements the list of streams is assembled in the business service and then handed over to the database service which does sth. like this:

INSERT INTO workstream(msgid, userid) VALUES(5, 1), VALUES(5, 2), ...;

This however can result in a huge amount of VALUES(…) parameters when the platform has many users. It didn’t occur as a problem to me when implementing it; PostgreSQL can handle large data, no? Well, it’s at least limited. When you use a JDBC prepared statement then the number of parameters has a hard limit: 32767. If you don’t obey this limit you’ll see something like this:

java.io.IOException: Tried to send an out-of-range integer as a 2-byte value: 40000

Not a very concrete message, right? When I first saw this exception I was kind of baffled and disaffected. But after narrowing down the problem in a debug session and looking at the PostgreSQL JDBC driver’s source code the cause was obvious: the PostgreSQL client/backend protocol dictates that the number of parameters be send from the client to the Postgres backend as a 2 byte integer (aaah, now the above message actually makes sense). You’ll find details of the protocol here if you’re brave (my 2-byte friend is defined in the Parse message).

All right, got the bugger, what’s the solution now? Easy: Just split the parameter list up into chunks of say 30.000 parameters per INSERT statement.


----------------------------------

问题:pg的jdbc driver对prepared Statement的参数 set的时候,client端的一个发送大小限制在2-byte。

解决方法:目前的版本最大限制50132,建议每次分割在50,000个参数去执行。

<think>好的,我现在需要帮助用户了解FIFO(先进先出)的概念和实现方式。首先,我需要确认用户的需求是什么。用户提到计算机科学中的FIFO实现或解释,可能是指数据结构中的队列,或者是其他应用场景比如缓存、调度算法等。不过根据用户提供的引用内容,特别是引用[1]中的队列操作示例,应该主要关注队列数据结构。 接下来,我需要整理FIFO的基本概念。FIFO即先进先出,队列就是典型的应用。需要解释队列的操作,如入队(enqueue)和出队(dequeue),以及它们的特性。可能还需要比较其他数据结构,比如栈(LIFO),以突出FIFO的特点。 然后,关于实现方式,用户可能需要不同编程语言的例子。引用[1]中的示例使用了类似Python的语法,但实际内容可能不是Python,因为Python的队列操作通常用deque或queue模块。但用户提供的示例中的myqueue.pop()输出2,3,这可能不符合Python列表的pop(0)操作,但可能用户只是举例。所以需要澄清不同语言的实现方式,比如Python中使用列表或collections.deque,Java中使用Queue接口,C++中使用<queue>库等。 另外,用户提供的引用[2]提到了工作项优先级,虽然与FIFO无直接关系,但可能需要注意到优先级队列和普通队列的区别,说明FIFO不考虑优先级,严格按照顺序处理。 需要确保回答符合系统级指令,比如数学公式的正确格式,但这个问题可能涉及较少数学内容,不过如果有涉及复杂度分析,比如队列操作的时间复杂度,可能需要使用LaTeX公式,例如$O(1)$。 然后,根据用户要求生成相关问题,可能需要包括队列的应用场景、实现方式的不同、复杂度分析,以及与类似结构的对比等。 最后,检查是否有引用需要添加。用户提供的引用[1]是关于队列操作的例子,可以在解释出队操作时引用;引用[2]关于优先级队列,可能在对比时提到,指出FIFO不涉及优先级,而优先级队列则不同,属于扩展内容。 总结回答结构:先解释FIFO概念,再描述队列操作,然后给出不同语言的实现示例,讨论应用场景,最后生成相关问题。需要确保每个部分简明清晰,符合用户要求的帮助解决问题。</think>### FIFO(先进先出)概念与实现 #### 1. 基本概念 FIFO(First-In-First-Out)是一种数据管理原则,**最早进入队列的元素最先被处理**。这种结构类似于现实生活中的排队场景,例如超市结账队列[^1]。 #### 2. 核心操作 - **入队(Enqueue)**:将元素添加到队列末尾。 - **出队(Dequeue)**:从队列头部移除元素。 - **特性**: - 时间复杂度:理想情况下入队和出队均为$O(1)$ - 空间复杂度:$O(n)$(n为队列长度) #### 3. 实现方式 **Python示例(使用列表):** ```python class Queue: def __init__(self): self.items = [] def enqueue(self, item): self.items.append(item) # 入队 def dequeue(self): return self.items.pop(0) if not self.is_empty() else None # 出队 # 示例操作 q = Queue() q.enqueue(1) q.enqueue(2) print(q.dequeue()) # 输出1(符合FIFO) ``` **Java实现(LinkedList):** ```java import java.util.LinkedList; import java.util.Queue; public class Main { public static void main(String[] args) { Queue<Integer> queue = new LinkedList<>(); queue.add(1); // 入队 queue.add(2); System.out.println(queue.poll()); // 输出1 } } ``` #### 4. 应用场景 1. **进程调度**:操作系统中的就绪队列 2. **数据缓冲**:网络数据包传输 3. **广度优先搜索(BFS)**:图的遍历算法 4. **打印任务管理**:按照提交顺序处理任务 #### 5. 对比其他结构 | 结构类型 | 操作特性 | 典型应用 | |---------|---------|---------| | 队列(FIFO) | 先进先出 | 任务调度 | | 栈(LIFO) | 后进先出 | 函数调用栈 | | 优先级队列 | 按优先级排序 | 实时系统任务处理[^2] |
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值