-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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
Potential resource leak in DataSourceUtils.doGetConnection [SPR-17559] #22091
Comments
Juergen Hoeller commented As far as I can see, the That said, the use of a logger in such a phase is debatable. No regular logger implementation would fail in such a scenario (it would rather defer/skip writing a log entry), so I don't see a problem in practice here, but defensively speaking we should avoid log statements at such a point. |
Juergen Hoeller commented I've removed affected debug log statements from |
Yurx commented I see connectionHandle calls releaseConnection (in holderToUse.setConnection). This could be overwritten and cause runtime exceptions. I see getTargetDataSource called on DataSource in new ConnectionSynchronization. I see ResourceHolder.setSynchronizedWithTransaction is not final and can be overwritten with a potential of runtime failures. Even if you remove log.debug from that method logging is used inside other methods called from there which may cause failures. To make it short I see not simply one or two simple assignments but a lot of code with a few cases that have real potential to cause resource leak. That is a textbook example of where to use try/catch block. Thinking defensively try/except/finally concept ensures both current implementation and future code changes are safe. I see this pattern is in use almost everywhere in that module only this code stands out. I don't see how this can be marked as complete without proper resource management put in place. |
Juergen Hoeller commented Fair enough, in custom cases there could be unexpected exceptions thrown... even if those probably indicate fatal misbehavior, some such exceptions may be recoverable. A defensive try-catch block doesn't hurt indeed, in addition to reduced log statements (which were inconsistently placed across our different resource utilities anyway). |
Juergen Hoeller commented I've added corresponding try-catch blocks to |
Yurx commented Yes sir now it does. Thank you! |
Yurx opened SPR-17559 and commented
Method doGetConnection in org.springframework.jdbc.datasource.DataSourceUtils has code that leaks (or has potential to leak) resources.
Objects like logger, DataSource, Connection, etc are used or expected to be referenced between the connection is created and returned. Use of those objects can raise exceptions and cause a connection to be created but not returned from the function. This prevents the caller from being able to release that connection.
The solution is simple and quite standard - try/catch clause is needed, see attachment.
This affects all recent versions including 5.0.x and 5.1.x
Affects: 5.0.11, 5.1.3
Attachments:
Backported to: 5.0.12, 4.3.22
The text was updated successfully, but these errors were encountered: