AEM stores data in a content repository. Applications can access the repository via either Sling ResourceResolver or JCR Sessions. The former is more high-level and more convenient as an API; the latter is more low-level and less readable, but more performant. Have a look at this article for a more detailed comparison. For this article, we’ll focus on Sling ResourceResolver.

Yes, we should always close ResourceResolver instances after we are done with them. (See 2nd pattern: Ownership: You open it — you close it paragraph in this blog post.) When instances are just left open, we have to deal with memory leaks.

The good news is that starting from AEM 6.2 we can leverage the try-with-resource idiom so that we never forget to close a resource resolver. Unfortunately, the org.apache.sling.api.resource.ResourceResolver class used in AEM versions before 6.2 don’t implement either java.io.Closeable or the java.lang.AutoCloseable interfaces. (Check uber-jar for 6.0.1 to see for yourself.)

However, for earlier AEM versions, we can still work around this by decorating the resource resolver to make it usable in try-with-resource constructs:

import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.sling.api.resource.ResourceResolverFactory;

public class ResourceResolverDecorator implements AutoCloseable {
    
    private static final Logger LOG = LoggerFactory.getLogger(ResourceResolverDecorator.class);
    
    public ResourceResolver resolver;
    boolean saveOnClose;
    
    public ResourceResolverDecorator(ResourceResolverFactory factory) throws LoginException {
        this(factory, true);
    }
    
    public ResourceResolverDecorator(ResourceResolverFactory factory, boolean saveOnClose) throws LoginException {        
        resolver = factory.getServiceResourceResolver(...);
        this.saveOnClose = saveOnClose;
    }
    
    public ResourceResolver getResolver() {
        return resolver;
    }

    @Override
    public void close() throws Exception {
        if (resolver != null && resolver.isLive()) {
            if (saveOnClose) {
                saveChanges();
            }
            resolver.close();
        }
    }
    
    public void saveChanges() {
        if (resolver.hasChanges()) {
            try {
                resolver.commit();
            } catch (PersistenceException e) {
                LOG.error(e.getMessage(), e);
            }
        }
    }
}

From here, we can simply create a resolver decorator within try(...), and Java will take care of calling the close() method automatically:
 

import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceResolver;
...

ResourceResolverFactory factory = ...;
try (ResourceResolverDecorator resolverDecorator = new ResourceResolverDecorator(factory)) {
            
    ResourceResolver resolver = resolverDecorator.getResolver();
    ...
            
} catch (Exception e) {
    LOG.error(e.getMessage(), e);
}