-
Notifications
You must be signed in to change notification settings - Fork 38.5k
AbstractCachingViewResolver - caching redirect views leads to memory leak [SPR-10065] #14698
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
Comments
Rossen Stoyanchev commented In the example from the referenced blog post, the same redirect URL can be expressed as a URI template, which would ensure the same view name is used as the cache key every time. In other words instead of: return "redirect:form.html?entityId=" + entityId; Do this: return "redirect:form.html?entityId={entityId}"; In the above example, entityId can be a model attribute or if it is present as a URI variable on the current request, then it'll work fine. In Spring 3.1+ it is actually preferable to use RedirectAttributes: @RequestMapping(method = RequestMethod.POST)
public String onPost(RedirectAttributes attrs) {
...
attrs.addAttribute(entityId, 123);
return "redirect:form.html; // resulting URL has entityId=123
} The above would also work before Spring 3.1 as long as entityId is in the model. However, using RedirectAttributes is preferable. See the reference documentation for more detail on that. In summary I think AbstractCachingViewResolver does what it's expected to do. It has a flag to disable caching but that should not be necessary. The main concern is that the memory leak can occur if a redirect URL is constructed in the controller and ends being different every time. However, I don't see an easy way to detect that. |
Juergen Hoeller commented It doesn't hurt to use a LinkedHashMap with a configurable cache limit (default 1024) here, similar to how we do it in CachingMetadataReaderFactory and NamedParameterJdbcTemplate. I have added this for 3.2 GA and 3.1.4 now. However, Rossen's advice still applies: Ideally, don't build custom redirect URLs with such changing ids through String concatenation. Just like you don't concatenate values into SQL Strings either but rather use PreparedStatements. Juergen |
Michał Jaśtak commented
Thank you all for correcting this very fast! |
Adedayo E commented
|
Rossen Stoyanchev commented Adedayo E, it has always worked that way for primitive model attributes (int, long, etc). From the Javadoc of RedirectView:
Also see RedirectTests.singleParam() for example. |
Adedayo E commented
|
Rossen Stoyanchev commented URI variables are not supported in redirect URLs before 3.1. Apologies if my comment was not clear but "the above would also work before Spring 3.1" was in reference to the preceding example, not all examples. |
Michał Jaśtak opened SPR-10065 and commented
When user uses URL prefixed with "redirect:" as the method invocation result in Controller, it is cached as the whole (with provided parameters) in AbstractCachingViewResolver. Because the parameters for redirect may vary for the same URL used in redirect, and HashMap based cache is used, that leads to memory leak.
PS: this problem exists also in 2.5.x, I didn't checked how far in the Spring history it reaches
Affects: 3.1.3
Reference URL: http://vard-lokkur.blogspot.com/2012/12/springs-web-mvc-redirect-to-memory-leak.html
Issue Links:
Referenced from: commits f19bc57, 9deaefe
Backported to: 3.1.4
1 votes, 7 watchers
The text was updated successfully, but these errors were encountered: