After deploying new Bundle: Forgetting to Close Resolver when Opened, is it cleared? | Community
Skip to main content
November 2, 2021
Solved

After deploying new Bundle: Forgetting to Close Resolver when Opened, is it cleared?

  • November 2, 2021
  • 3 replies
  • 964 views

Our AEM website has many error/warning signaling us that a resolver has not been closed, when it is opened.

When we deploy a new version Bundle, will all these messages goes away? or would we be forced to restart AEM? How can we close all the resolvers, and continue with AEM's website without restart?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Asutosh_Jena_

Hi @supportmember 

 

In Java 7 the idiom “try-with-resource” has been introduced in the java world. It helps to never forget to close a resource. And since Sling 9, the ResourceResolver interface implements the AutoCloseable marker interface, so the try-with-resource idiom can be used.

 

That means, that you can and should use this approach:

try (ResourceResolver resolver = getResourceResolver) {
// do something with the resolver
// no need to close it explicitly, it's closed automatically
}

 

With this approach you omit the otherwise obligatory finally block to close the resource resolver (which can be forgotten...)

 

This approach helps to reduce boilerplate code and eliminates some potential for errors.

 

When you redeploy the code, it will restart the bundle only specific to the project, but the complete OSGi service will not be restarted. It will not close the current threads and requires a restart. You can see the SessionStatistics objects in the JMX console(http://localhost:4502/system/console/jmx).

 

So after applying the fix, you will need to do a restart of the instance which will restart the complete OSGi service by closing all the ongoing/blocked threads.

 

Thanks!

 

3 replies

Siva_Sogalapalli
Community Advisor
Community Advisor
November 2, 2021

We had similar issues in the past and the reason was there were many sessions or resolvers opened and but not closed properly.

In such case, these kind of issue happens & over a period of time we need to restart AEM or redeploy bundles.

 

To fix this, you need to review the code and close all unclosed sessions/resource resolvers. 

 

The best practise is, make sure to declare Sessions or ResourceResolvers locally and close them as soon as you don't need.

 

example: 

finally {
if (session != null && session.isLive()) {
    session.logout(); 
}
if (resolver != null && resolver.isLive()) {
    resolver.close();
}
}

 

Hope, this would resolve your issues permanently.

 

Thanks

 

 

 

November 2, 2021

Thank you, we have done this, but the question is. When we deploy a new bundle, does these threads get closed?

Asutosh_Jena_
Community Advisor
Asutosh_Jena_Community AdvisorAccepted solution
Community Advisor
November 3, 2021

Hi @supportmember 

 

In Java 7 the idiom “try-with-resource” has been introduced in the java world. It helps to never forget to close a resource. And since Sling 9, the ResourceResolver interface implements the AutoCloseable marker interface, so the try-with-resource idiom can be used.

 

That means, that you can and should use this approach:

try (ResourceResolver resolver = getResourceResolver) {
// do something with the resolver
// no need to close it explicitly, it's closed automatically
}

 

With this approach you omit the otherwise obligatory finally block to close the resource resolver (which can be forgotten...)

 

This approach helps to reduce boilerplate code and eliminates some potential for errors.

 

When you redeploy the code, it will restart the bundle only specific to the project, but the complete OSGi service will not be restarted. It will not close the current threads and requires a restart. You can see the SessionStatistics objects in the JMX console(http://localhost:4502/system/console/jmx).

 

So after applying the fix, you will need to do a restart of the instance which will restart the complete OSGi service by closing all the ongoing/blocked threads.

 

Thanks!

 

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 3, 2021

@supportmember,
I am afraid after when a new bundle have been deployed, the JVM is still running, which means that these threads are not closed. Restart your AEM to close all your open threads.

@asutosh_jena_'s recommendation try-with-resource trick introduced in Java 7  will ensure that the session is closed after usage.