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

NamedParameterJdbcTemplate has the potential to leak memory [SPR-7237] #11896

Closed
spring-projects-issues opened this issue May 25, 2010 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Andrew Robinson opened SPR-7237 and commented

org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate has the potential to leak memory via its parsedSqlCache property.

The code only ever adds to the Map and does not limit its size in any way.

Nothing in the Javadoc makes developers aware that this Map could potentially grow without bounds.

We encountered this problem only under live load. The search on our website has the potential to generate a large number of permutations. We were thus dynamically generating a large number of unique SQL statements.
We do use bind params where possible but there are places where this is difficult or inconvenient.

The size of these statements as String keys, stored within NamedParameterJdbcTemplate, caused a slow memory leak that gave us OutOfMemoryError after about 12 hours of live traffic.

We are using 3.0.1.RELEASE but from Fisheye it looks like this behaviour is still present in your trunk.

The following seem like options, please do as you see fit:

a) Update the Javadoc with a warning about this behaviour
b) Limit the max size of the Map, configurable but with sensible defaults
c) use a WeakReference or SoftReference HashMap

Our workaround has been to extend the class and override the method as follows:

/**
 * @return the uncached, parsed SQL statement
 */
protected ParsedSql getParsedSql(String sql) {
	return NamedParameterUtils.parseSqlStatement(sql);
}

Affects: 3.0.1

Referenced from: commits 2136b04

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Good point. I've added a cache limit of 256 entries, configurable through the setCacheLimit method.

This will be available in tomorrow's 3.0.3 snapshot. Feel free to give it an early try...

Juergen

@spring-projects-issues spring-projects-issues added type: bug A general bug in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.0.3 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants