Skip to content

Fix Statement leak in Trino JDBC DatabaseMetaData#10632

Merged
findepi merged 5 commits intotrinodb:masterfrom
findepi:findepi/jdbc-statement-leak
Jan 18, 2022
Merged

Fix Statement leak in Trino JDBC DatabaseMetaData#10632
findepi merged 5 commits intotrinodb:masterfrom
findepi:findepi/jdbc-statement-leak

Conversation

@findepi
Copy link
Copy Markdown
Member

@findepi findepi commented Jan 15, 2022

Before the change, Trino JDBC's DatabaseMetaData implementation
(TrinoDatabaseMetaData) would create Statement objects that are
never closed. Since Connection (TrinoConnection) tracks open
statements to be able to close them upon Connection.close() (per JDBC
requirements), this created a memory leak where Statement objects are
leaked in Connection.statements collection.

The commit fixing this, under the condition that ResultSet returned
from TrinoDatabaseMetaData is correctly closed.

Fixes #10584

@cla-bot cla-bot bot added the cla-signed label Jan 15, 2022
@findepi findepi force-pushed the findepi/jdbc-statement-leak branch from 7d3baed to b5f7337 Compare January 15, 2022 10:27
@findepi findepi added the jdbc Relates to Trino JDBC driver label Jan 15, 2022
Copy link
Copy Markdown
Member

@electrum electrum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this is better than my first idea of a weak reference

`AsyncIterator.cancel` is idempotent (future cancellation) and
`client.close()` is idempotent too (state atomic swap controls that), so
this is not a behavioral change, but necessary for further code
evolution.
It's somewhat more readable if the mutable state, especially one that
needs to be updated with thread-safety in mind, is confined to a class.
Before the change, Trino JDBC's `DatabaseMetaData` implementation
(`TrinoDatabaseMetaData`) would create `Statement` objects that are
never closed. Since `Connection` (`TrinoConnection`) tracks open
statements to be able to close them upon `Connection.close()` (per JDBC
requirements), this created a memory leak where `Statement` objects are
leaked in `Connection.statements` collection.

The commit fixing this, under the condition that `ResultSet` returned
from `TrinoDatabaseMetaData` is correctly closed.
@findepi findepi force-pushed the findepi/jdbc-statement-leak branch from b5f7337 to bea351b Compare January 17, 2022 11:51
@findepi
Copy link
Copy Markdown
Member Author

findepi commented Jan 18, 2022

CI #10631 (comment)

@findepi findepi merged commit 96287a6 into trinodb:master Jan 18, 2022
@findepi findepi deleted the findepi/jdbc-statement-leak branch January 18, 2022 08:15
@github-actions github-actions bot added this to the 369 milestone Jan 18, 2022
@findepi findepi mentioned this pull request Jan 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed jdbc Relates to Trino JDBC driver

Development

Successfully merging this pull request may close these issues.

Memory leak in Trino JDBC driver when using DatabaseMetaData

4 participants