-
Notifications
You must be signed in to change notification settings - Fork 134
VELOCITY-989 Fixed multiple connection leaks in DataSourceResourceLoader. #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
The first case was the implementation of FilterReader would get the connection from a result set after closing the result set. This always throws an exception as it is an invalid operation after closing the result set. The second is in the getResourceReader method. In the happy path a FilterReader is returned which eventually closes the connection when close is called. However, if a template is not found, an exception is thrown without closing the connection.
|
I wonder why this never surfaced before... |
|
First, correction to my first comment, the original issue was attempting to get a statement from a result set, which then was used to get the connection to close. Getting the statement was an invalid operation after closing a result set. This PR doesn't fix the issue depending on what JDBC implementation you are using. It does not seem like it's safe to rely on Statement.getConnection at all rather then keeping a reference to the original connection returned from DataSource.getConnection. org.apache.tomcat.jdbc.pool: org.apache.commons.dbcp2: The NPE issue could be handled with a null check but I don't see any way around the issue with org.apache.tomcat.jdbc.pool without keeping a reference to the connection. There are probably still more other unknown issues since the behavior of ResultSet.getStatement and Statement.getConnection does not appear to be standardized and varies across implementations. I have a custom DataSourceResourceLoader I am now using which does not rely on Statement.getConnection and can update this PR if it seems like this issue will get any traction. |
The DatasourceResourceLoader has always been a second zone citizen, not well tested, not well documented, and its javadoc states that it's a simple loader. |
…redStatement.getConnection and ResultSet.getStatement will return the wrapped objects created by the underlying DataSource and not return null or throw an exception when closed. Removed clumsy TestDefaultDatabaseObjectsFactory.java that was used to test for connection leaks and instead added 3 new tests that use commonly pooled DataSource implementations.
|
XPosting from the jira issue: However I would prefer to change DatabaseObjectsFactory.prepareStatement and DatabaseObjectsFactory.releaseStatement to use an explicit wrapper object instead. This would avoid the somewhat hard to follow proxy code and would be a bit more obvious to anyone writing custom implementations of DatabaseObjectsFactory that the original connection and statement are needed rather then relying on Statement.getConnection or ResultSet.getStatement. |
|
@arkanovicz has the expertise to review this. |
The first case is in the implementation of FilterReader. It attempts to get a connection from a result set after closing the result set. This always throws an exception as it is an invalid operation after closing the result set, which results in not closing the connection.
The second is in the getResourceReader method. In the happy path a FilterReader is returned which eventually closes the connection when close is called. However, if a template is not found, an exception is thrown without closing the connection.
Opened issue here https://issues.apache.org/jira/browse/VELOCITY-989