Expand my Community achievements bar.

SOLVED

ResourceResolver Closure in Servlet

Avatar

Level 4

Hi Team,

 

I have created ResourceResolver from request object of the Servlet. Do we need to close it manually in the Servlet?

 

1) PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class);

2) Session session=request.getResourceResolver().adaptTo(Session.class);

String  newurl =  "/jcr:content/root/responsivegrid/responsivegrid/login";
3) Resource resource = request.getResourceResolver().getResource(newurl);

 

 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

@shyamasundar-ks 

I think getResourceResolver() does not create a new ResourceResolver; as a best practice, we recommend using the try-with-resources statement that ensures that each resource is closed at the end of the statement, with the most commonly used way to make a new ResourceResolver i.e ResourceResolverFactory.

More info here- https://cqdump.joerghoh.de/2018/11/14/try-with-resource-or-i-will-never-forget-to-close-a-resource-r... 

View solution in original post

2 Replies

Avatar

Community Advisor

I think, no need to close it explicitly.

 

Avatar

Correct answer by
Employee Advisor

@shyamasundar-ks 

I think getResourceResolver() does not create a new ResourceResolver; as a best practice, we recommend using the try-with-resources statement that ensures that each resource is closed at the end of the statement, with the most commonly used way to make a new ResourceResolver i.e ResourceResolverFactory.

More info here- https://cqdump.joerghoh.de/2018/11/14/try-with-resource-or-i-will-never-forget-to-close-a-resource-r...