简体   繁体   中英

closeOnCompletion doesn't work properly in MySQL JDBC 5.1.32

I have a snippet like this:

Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM table_A");

statement.closeOnCompletion();
System.out.println(statement.isCloseOnCompletion());

rs.close();
System.out.println(statement.isClosed());

After closeOnCompletion is invoked, I expect the statement to be closed, as its result set is closed. However, it just shows "false" when checking statement.isClosed().

Did I misunderstand the jdk doc? as it says: closeOnCompletion() Specifies that this Statement will be closed when all its dependent result sets are closed.

=========================== Update: It turns out to be my carelessness. The last statement actually returns a result of expected "true". So it's no longer an issue

Since Statement is depend on result I would suggest this

Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM table_A");

rs.close();
statement.close();  
System.out.println(statement.isClosed());

This is applicable if your ultimate want is to close statement, if your question is why statement is not closing even when called onexit, then better to refer javadocs.

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