Should I close the session before closing the resource resolver

拥有回忆 提交于 2019-12-20 02:59:20

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!