问题
I have simple service, lets use resourceResolver and session for some logic:
@Component(immediate = true)
@Service(value = ServiceInterface.class)
public class ServiceInterfaceImpl implements ServiceInterface {
//Some fields
@Reference
private ResourceResolverFactory resolverFactory;
private void someMethod() {
ResourceResolver resourceResolver = null;
try {
resourceResolver = resolverFactory.getServiceResourceResolver(null);
Session session = resourceResolver.adaptTo(Session.class)
someMethod2(resourceResolver);
someMethod3(session);
} catch (LoginException e) {
log.error(e.getMessage(), e);
} finally {
if (resourceResolver != null && resourceResolver.isLive()) {
resourceResolver.close();
}
}
}
//Some implementation
}
Should I close the session, or it will be closed in automatically resourceResolver.close()
?
回答1:
The session will be automatically closed, when you close the ResourceResolver, so resourceResolver.close() is enough. You can dig into the code to find the place where this happens. If you get a session repo.loginAdministrative(), you should logout the session at the end, but this is not the recommended way to obtain a jcr session.
来源:https://stackoverflow.com/questions/37880336/should-i-close-the-session-before-closing-the-resource-resolver