每个结果只应调用一次getResultSet()" [英] getResultSet() "should be called only once per result"

查看:93
本文介绍了每个结果只应调用一次getResultSet()"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档对于 getResultSet java.sql.Statement 中,它说:


以ResultSet对象的形式检索当前结果。这个方法应该每个结果只调用

Retrieves the current result as a ResultSet object. This method should be called only once per result.

使用一些测试代码,我运行 executeQuery()和几次调用 getResultSet()并观察到 ResultSet 返回指向到同一个对象。所以我猜它没有返回一个你需要单独关闭的不同的 ResultSet 。但当然这对我的JDBC驱动程序来说可能是独一无二的。

Using some test code, I ran executeQuery() and several calls to getResultSet() and observed that the ResultSet returned pointed to the same object. So I'm guessing it is not returning a different ResultSet which you would need to close individually. But of course this could be unique to my JDBC drivers.

查看文档 ResultSet 它说:


默认的ResultSet对象不可更新,并且只有
向前移动的游标。因此,您只能迭代一次,
仅从第一行到最后一行。

A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row.

这似乎是一个很好的理由为什么多次调用它可能不是一个好主意,因为它可能导致一些陷阱的情况。如果这是唯一的原因,我觉得他们可能刚刚说过,所以我认为可能还有更多不仅仅是这个。

This seems like a good reason why it may not be a good idea to call it multiple times as it may lead to some "gotcha" situation. If this was the sole reason, I felt that they could have just said that so I think there might be more to it than just this.

所以有人知道为什么每个结果不应该多次调用 getResultSet ?这个问题首先让我感到好奇。

So does anyone know why one shouldn't call getResultSet more than once per result? This question is what made me curious in the first place.

推荐答案

ResultSet 对象是Java JDBC提供的接口 - 他们这样做不提供实施。即使你的特定数据库代码和相关的驱动程序实现了 ResultSet ,这样你可以每个结果多次调用它,如果你依赖于这样的行为,那么在合同之外,你当然是玩火。

The ResultSet object is an interface provided by Java JDBC -- they do not provide an implementation. Even though your particular database code and associated drivers implement ResultSet so that you can call it multiple times per result, if you depend on such behavior that is outside of the contract, you are certainly playing with fire.

合同是用写的一个可能的原因这个方法应该只调用一次每个结果行是出于效率原因。构建ResultSet很可能会对数据库进行JDBC RPC调用,而JDBC规范的作者则希望阻止多次往返。他们可能不想强迫实施者有效地防止每个结果多次调用。同样,即使您的数据库正在防止该行为,也不意味着下一个行为。

One possible reason that the contract was written with the this method should be called only once per result line is for efficiency reasons. Building the ResultSet will most likely make a JDBC RPC call to the database and the authors of the JDBC specification wanted to discourage multiple round trips. They may not have wanted to force implementers to protect against multiple calls per result efficiently. Again, even though your database is protecting against that behavior does not mean the next one will.

大多数 ResultSet 实现还保持与数据库的连接打开,这样当你获得某些字段(例如大blob)时,它可以回调数据库来获取数据。打开多个连接或(更糟)使用来自多个 ResultSet 对象的相同连接会非常危险/混乱。

Most ResultSet implementations also hold a connection to the database open so that when you get certain fields (such as large blobs) it can call back to the database to get the data. Having multiple connections open or (worse) using the same connection from multiple ResultSet objects would be very dangerous/confusing.

此外,他们可能一直担心代码的两个部分调用 getResultSet()两次,并返回对同一个未同步对象的引用。当调用 next()并用多个引用覆盖对象时,这会引起混淆。

Also, they may have been worried about two parts of your code calling getResultSet() twice and were returned a reference to the same single unsynchronized object. This would cause confusion when next() was called and overwrote the object with multiple references.

我在推测当然,但我希望这会有所帮助。

I'm speculating of course but I hope this helps.

这篇关于每个结果只应调用一次getResultSet()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆