我有简单的服务,让我们使用resourceResolver和session来获取一些逻辑:
@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
}
我应该关闭会话,还是会自动关闭resourceResolver.close()
?
答案 0 :(得分:4)
当您关闭ResourceResolver时,会话将自动关闭,因此resourceResolver.close()就足够了。您可以深入研究代码以找到发生这种情况的地方。如果你得到一个会话repo.loginAdministrative(),你应该在结尾注销会话,但这不是获得jcr会话的推荐方法。