VELOCITY-989 Fixed multiple connection leaks in DataSourceResourceLoader.#57
VELOCITY-989 Fixed multiple connection leaks in DataSourceResourceLoader.#57FrancisS wants to merge 6 commits intoapache:masterfrom
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. |
|
Any chance this will get reviewed or should I just close it? |
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