GetResultSet () "should be called only once per result"

According to the documentation for getResultSet in getResultSet , it says:

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

Using some test code, I ran executeQuery() and several calls to getResultSet() and noticed that the returned ResultSet points to the same object. Therefore, I assume that it does not return another ResultSet , which you will need to close individually. But of course, this may be unique to my JDBC drivers.

Looking at the documentation on the ResultSet , he says:

The ResultSet object is not updated by default and has a cursor that is only moving forward. Thus, you can go through it only once and only from the first line to the last line.

This seems like a good reason why it might not be a good idea to name it several times, as this may lead to some β€œreceive” situation. If that was the only reason, I felt that they could just say that I think there could be more than just that.

So does anyone know why you can't call getResultSet more than once per result? This is a question that made me curious in the first place.

+6
source share
1 answer

The ResultSet object is the interface provided by Java JDBC - they do not provide an implementation. Despite the fact that your specific database code and related drivers implement ResultSet so that you can name it several times for each result, if you depend on such behavior that is outside the contract, you are definitely playing with fire.

One of the possible reasons the contract was written using the string this method should be called only once per result , for efficiency reasons. Building a ResultSet is likely to trigger a JCBC RPC call into the database, and the JDBC spec authors would like to beat off multiple trips. Perhaps they did not want to force the performers to effectively protect against several calls for the result. Again, even if your database protects against this behavior, this does not mean that the next will be.

Most ResultSet implementations also support connecting to an open database, so when you get certain fields (like big drops), it can call back to the database to receive the data. Having multiple connections open or (worse) using the same connection from multiple ResultSet objects will be very dangerous / confusing.

In addition, they may have been bothered by the two parts of your code calling getResultSet() twice, and references to the same single unsynchronized object were returned. This can cause confusion when calling next() and overwrite the object with multiple references.

I think, of course, but I hope this helps.

+4
source

Source: https://habr.com/ru/post/912268/


All Articles