Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

AEMWizard
Level 3
Level 3

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
Asutosh_Jena_
Correct answer by
Community Advisor
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
SivaPrasadReddyS
Level 7
Level 7

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

 

 

 

AEMWizard
Level 3
Level 3

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

Asutosh_Jena_
Correct answer by
Community Advisor
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

BrianKasingli
Community Advisor
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.