Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

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

Avatar

Level 6

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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @AEMWizard 

 

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!

 

View solution in original post

4 Replies

Avatar

Community Advisor

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

 

 

 

Avatar

Level 6

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

Avatar

Correct answer by
Community Advisor

Hi @AEMWizard 

 

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!

 

Avatar

Community Advisor

@AEMWizard,
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.