Java JDBC:Reply. fill()

I sometimes get the following exceptions:

The problem is that the code executes successfully for a period of time, and then suddenly I get this exception However, when I run the code again, it will work properly

Someone can tell me the possible problems and provide some solutions

Solution

This is a sign that JDBC resources are not properly closed / released You need to get and close all JDBC resources in the shortest possible range, that is, you need to close them in the reverse order in the final block of the try block of the method block exactly the same as getting them For example

Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
    connection = database.getConnection();
    statement = connection.createStatement();
    resultSet = statement.executeQuery(sql);
    // ...
} finally {
    if (resultSet != null) try { resultSet.close(); } catch (sqlException logorIgnore) {}
    if (statement != null) try { statement.close(); } catch (sqlException logorIgnore) {}
    if (connection != null) try { connection.close(); } catch (sqlException logorIgnore) {}
}

If you don't shut them down correctly as soon as possible, the database will hold it in your hand sooner or later, and your application may break sooner or later when you meet yourself

To improve connection performance, use connection pools – you still need to get and close them in the same way as above! It's just a connection pool implementation now. It's worried about actually closing the connection under the hood

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>