Skip to content
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

Find a better way to keep JdbcDaoFactory's PreparedStatementSource cache from leaking memory #2739

Open
rbeckman-nextgen opened this issue May 11, 2020 · 1 comment

Comments

@rbeckman-nextgen
Copy link
Collaborator

JdbcDao uses CachedPreparedStatementSource objects to cache PreparedStatements to improve performance. Since a connection pool is also used, we must create a CachedPreparedStatementSource object for each connection obtained from the pool. The cache objects are mapped to connections in a Map object called statementSources in JdbcDaoFactory.

Occasionally, connections become closed and are replaced by new connections in the connection pool. When this happens, we must remove those connections from the statement cache map to prevent a memory leak.

Currently, when a new connection is put into the map, we then test all of the existing connections to see if any have been closed and then remove them.

Ideally, we would remove the connection from the map immediately once it has been closed. However we don't currently know of a way to add some sort of hook to do that.

It may be worth investigating potential ways to detect immediately when a connection is closed so the connection can then be removed from the map.

Imported Issue. Original Details:
Jira Issue Key: MIRTH-2811
Reporter: brentm
Created: 2013-07-30T10:12:05.000-0700

@rbeckman-nextgen rbeckman-nextgen added this to the 3.10.0 milestone May 11, 2020
@rbeckman-nextgen
Copy link
Collaborator Author

I believe in JdbcDaoFactory you can just change
private Map<Connection, PreparedStatementSource> statementSources = new ConcurrentHashMap<Connection, PreparedStatementSource>();
to
private Map<Connection, PreparedStatementSource> statementSources = new com.google.common.collect.MapMaker().weakKeys().makeMap();

The Guava library is already a dependency of donkey. This will return a java.util.concurrent.ConcurrentMap with weak key references as in a java.util.WeakMap.

This way, once the connection pool has discarded its reference to the connection, the JdbcDaoFactory is only holding a weak reference to it, and it will become eligible for garbage collection. The map takes care of auto-removing keys that have been garbage collected.

Imported Comment. Original Details:
Author: agermano
Created: 2019-11-07T07:03:35.000-0800

@cturczynskyj cturczynskyj removed this from the 3.10.0 milestone Nov 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants