Statement leak on the connector datasource?

1
0
-1

Hello,

On the project I am working on, we detect a "Too Many Open Cursor" on a data source connector. This connector access anc Oracle Database.

Looking at the data source Connector source

https://github.com/bonitasoft/bonita-connector-database/blob/master/src/main/java/org/bonitasoft/connectors/database/datasource/DatasourceConnector.java

In line 138:

if (command.startsWith("SELECT")) {
resultSet = database.select(script);
result.put("resultset", resultSet);
}
In the disconnect:

public void disconnect() throws ConnectorException {
if (resultSet != null) {
try {
resultSet.close();
} catch (Exception e) {
throw new ConnectorException(e);
}
}

So, assuming we pass correctly in the disconnect every time (even when the setOutputParameters() failed with an exception ?), the ResultSet is close.

But what about the statement ? Database.select() do

public ResultSet select(final String query) throws SQLException {
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
return statement.executeQuery(query);
}

This statement is never close explicitly then.

So questions are:

1/ if the setOuputParameters() failed, is the disconnect() is correctly cause ?

2/ the statement.close() is not called. Is that not an issue? If the statement.close() is called when the object is deleted, then it depends on the Garbage Collector. Before this guy runs, a lot of statements may be waiting, causing the issue.

Indeed, in the database connector, the connection is close at the disconnect(). But on the datasource connector, the connection.close() does not close the connection, just release it to the pool. Any open resources are kept.

3/What happens if, in the groovy, a developer does

"resultset=null".

Is that impact the variable on the Java side? If yes, then the disconnect() will never close the ResultSet.

1 answer

1
+1
-1
This one is the BEST answer!

Good catch!

It is always better to explicitly close Connection, ResultSet and Statement. Moreover this code could benefit from using Java try-with-resources Statement

Could you open a Jira Ticket, so that the team could make the improvement, or even better a Pull Request on the project?

Captain Bonita

Notifications