简体   繁体   中英

java.sql.ResultSet: flawed by design?

This is the signature of java.sql.ResultSet.next() :

public boolean next() throws SQLException

As you all know, the method returns true if more rows are available into the ResultSet . But what if a SQLException is thrown? There's no way for me to obtain next() 's return value, so there's no way for me to know if more records are available (there's no trace of a hasNext method into the interface).

Let's switch perspective. I'm working on a JDBC driver that needs to be fault tolerant (it reads from csv). How can I let my users know if more rows are available after a wrong row?

There is nothing wrong in that API design. next() - Moves the cursor froward one row from its current position. So a JDBC driver has just a cursor for the result set. It gets every row from the database via that cursor every time you call next() method. It is only logical to hit some "Exceptions" in scenarios dealing with very large number of rows as part of querying a table.

Since you are "designing" your own csv based driver, you are free to alter the next() API's behavior . Though the signature states that it might throw SQLException , you may code the next method's implementation such that it returns an empty or partially filled object in order to make it fault tolerant.

There are obsolete techniques to find total number of records in a ResultSet - such as calling last() and then calling ResultSet::getRow() to find out count of records before you begin to iterate.

It's up to you to decide whether your implementation actually throws an exception from these methods, just because it's on the interface doesn't oblige you to throw it. But if something goes wrong where your driver has trouble figuring out whether there is a next row, I doubt it is going to be recoverable. I mean, if your underlying code throws an IOException for whatever reason (network access, permission, hardware glitch, some other process deleted the file out from under you), throwing an exception may be the reasonable thing to do.

You can use isLast() to determine whether you are on the last row. However, this will also thrown an SQLException .

while most agree that JDBC is polluted with (checked!) exceptions everywhere, the meaning of the exception is "Sorry, I don't know, this error occurred", that's why it does not return a value when an exception is raised. Such a case is "i lost connection", or something even worse happened.

If you know there is a row after that one, return true.

If that next row is a malformed one, throw an exception when the malformed row is fetched, even if you would be able to detect it during execution of the "next()" method.

SQLException should here only be thrown if a database access error occurs or this method is called on a closed result set. (see ResulSet's Javadoc)

In both cases you do not want to go on using the result set. You may want try to close it if the exception occurs and afterwards throw it away.

I cannot see a problem here: Either you've a next row or not. And when problems occur then throw the Exception.

If you want something that recovers from Exceptions here then build something around it rather than trying to "break" the common behaviour of these interfaces.

But maybe you should think about to not implement a JDBC driver for reading CSV-Data first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM