我应该在关闭资源解析器之前关闭会话

我应该在关闭资源解析器之前关闭会话

本文介绍了我应该在关闭资源解析器之前关闭会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务很简单,让我们使用resourceResolver和session来实现一些逻辑:

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
 }

我应该关闭会话,还是会在resourceResolver.close()中自动关闭?

Should I close the session, or it will be closed in automatically resourceResolver.close()?

推荐答案

关闭ResourceResolver时,会话将自动关闭,因此resourceResolver.close()就足够了.您可以深入研究代码以查找发生这种情况的位置.如果获得会话repo.loginAdministrative(),则应在最后注销该会话,但这不是获取jcr会话的推荐方法.

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.

这篇关于我应该在关闭资源解析器之前关闭会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 16:13