J Deckarm wrote:=> It seems this is dependent on the type of the closed object. For me only stmt.close() followed by stmt.***Warnings() seem to produce "SQLRecoverableException: Closed Statement". But e.g. conn.close() followed by conn.***Warnings(), or stmt.close() followed by rs.***Warnings() do not seem to produce any exception
First of all, I checked the javadoc for these methods on all 3 objects: Connection, Statement, and ResultSet.
Here's a snippet of the javadoc of the
Connection interface for both the
clearWarnings() and
getWarnings() methods:
Java 7 API Specification, Connection wrote:void clearWarnings() throws SQLException
Throws:
SQLException - SQLException if a database access error occurs or this method is called on a closed connection
Java 7 API Specification, Connection wrote:SQLWarning getWarnings() throws SQLException
Throws:
SQLException - if a database access error occurs or this method is called on a closed connection
If you look at the
Statement and
ResultSet interfaces you can read similar descriptions for both the
clearWarnings() and
getWarnings() methods. So I understand that's what a study guide mentions in its text, because that's how those methods should behave and that's what you need to know for the exam.
Then I had a go as well and used MySql (mysql-connector-java-5.1.36). My first attempt was on a closed result set
Output:
SQLException: Operation not allowed after ResultSet closed
SQLState: S1000
VendorError: 0
Second attempt on a closed statement
Output:
SQLException: No operations allowed after statement closed.
SQLState: S1009
VendorError: 0
Finally, the third attempt on a closed connection
But this code didn't throw a SQLException. So I got a little but curious and decided to look at the implementation of the
getWarnings() method of the
Connection interface. I was a bit surprised when I opened the
com.mysql.jdbc.ConnectionImpl and noticed this
getWarnings() implementation
It just returns
null. So that explains pretty well why I didn't get a SQLException, although I should have got one according to the API
Hope it helps!
Kind regards,
Roel