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

Should I close the session before closing the resource resolver

我有简单的服务,让我们使用 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()

会话会自动关闭,当你关闭 ResourceResolver 时,所以 resourceResolver.close() 就足够了。您可以深入研究代码以找到发生这种情况的地方。如果你得到一个会话repo.loginAdministrative(),你应该在最后注销会话,但这不是获得jcr会话的推荐方法。