在CQ5

时间:2015-06-04 07:11:46

标签: cq5 aem jcr

我在error.log中收到未关闭会话检测到的警告。我打开并关闭了finally块中的所有会话。我仍然收到警告。由于大量未关闭的会话,它使作者实例非常缓慢。以下是错误日志

04.06.2015 02:45:54.921 *WARN* [Finalizer] org.apache.jackrabbit.core.SessionImpl Unclosed session detected. The session was opened here:  java.lang.Exception: Stack Trace
        at org.apache.jackrabbit.core.SessionImpl.<init>(SessionImpl.java:222)
        at org.apache.jackrabbit.core.XASessionImpl.<init>(XASessionImpl.java:117)
        at com.day.crx.core.CRXSessionImpl.<init>(CRXSessionImpl.java:69)
        at com.day.crx.core.CRXRepositoryImpl.createSessionInstance(CRXRepositoryImpl.java:935)
        at org.apache.jackrabbit.core.RepositoryImpl.createSession(RepositoryImpl.java:959)
        at org.apache.jackrabbit.core.SessionFactory.createAdminSession(SessionFactory.java:42)
        at com.day.crx.sling.server.impl.SlingRepositoryWrapper.loginAdministrative(SlingRepositoryWrapper.java:76)
        at org.apache.sling.jcr.resource.internal.helper.jcr.JcrResourceProviderFactory.getResourceProviderInternal(JcrResourceProviderFactory.java:136)
        at org.apache.sling.jcr.resource.internal.helper.jcr.JcrResourceProviderFactory.getAdministrativeResourceProvider(JcrResourceProviderFactory.java:115)
        at org.apache.sling.resourceresolver.impl.tree.ResourceProviderFactoryHandler.login(ResourceProviderFactoryHandler.java:162)
        at org.apache.sling.resourceresolver.impl.tree.RootResourceProviderEntry.loginToRequiredFactories(RootResourceProviderEntry.java:95)
        at org.apache.sling.resourceresolver.impl.ResourceResolverFactoryImpl.getResourceResolverInternal(ResourceResolverFactoryImpl.java:95)
        at org.apache.sling.resourceresolver.impl.ResourceResolverFactoryImpl.getAdministrativeResourceResolver(ResourceResolverFactoryImpl.java:69)
        at org.apache.sling.resourceresolver.impl.helper.ResourceResolverContext.getResourceTypeResourceResolver(ResourceResolverContext.java:192)
        at org.apache.sling.resourceresolver.impl.helper.ResourceResolverContext.getParentResourceType(ResourceResolverContext.java:216)
        at org.apache.sling.resourceresolver.impl.ResourceResolverImpl.getParentResourceType(ResourceResolverImpl.java:1136)
        at org.apache.sling.resourceresolver.impl.ResourceResolverImpl.getParentResourceType(ResourceResolverImpl.java:1126)
        at org.apache.sling.resourceresolver.impl.ResourceResolverImpl.isResourceType(ResourceResolverImpl.java:1151)
        at org.apache.sling.api.resource.ResourceUtil.isA(ResourceUtil.java:466)
        at com.adobe.cq.social.storage.index.AbstractBaseIndexHandler.checkResourceType(AbstractBaseIndexHandler.java:111)

这是我们收到此警告的代码段。

private List<String>  rollOutPages(Collection<Page> pages) throws Exception
{
  final ResourceResolver adminResourceResolver=resourceResolverFactory.getAdministrativeResourceResolver(null);
        List<String> rolledOutPages = new ArrayList<>();
        try {
            //adminResourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
            for (Page page : pages) {
                final Collection<LiveRelationship> liveRelationships = relationShipManager.getLiveRelationships(page, null, null, false);
                for (LiveRelationship relationship : liveRelationships) {
                    rolloutManager.rollout(adminResourceResolver, relationship, false);
                    rolledOutPages.add(relationship.getTargetPath());
                }

            }
            adminResourceResolver.adaptTo(Session.class).save();
            adminResourceResolver.adaptTo(Session.class).logout();
            adminResourceResolver.close();
            return rolledOutPages;
        }
        catch (WCMException e) {
            adminResourceResolver.adaptTo(Session.class).save();
            adminResourceResolver.adaptTo(Session.class).logout();
            adminResourceResolver.close();
            LOG.error("Unable to create administrative resource resolver", e);
            throw new AbortException("Exception during rollout of pages",e.getMessage());
        }
        catch (Exception e)
        {
            adminResourceResolver.adaptTo(Session.class).save();
            adminResourceResolver.adaptTo(Session.class).logout();
            adminResourceResolver.close();
            LOG.error("Exception during rollout of pages", e);
            throw new AbortException("Exception during rollout of pages",e.getMessage());
        }
        finally {
            if(adminResourceResolver.isLive()||adminResourceResolver != null){
                adminResourceResolver.close();
            }
        }

    }

我在finally块中关闭了会话。仍然得到这个警告。有人可以帮我理解我在这里缺少什么吗?

1 个答案:

答案 0 :(得分:0)

我看到你正在关闭ResourceResolver,而不是你在ResourceResolver上调用adaptTo(Session.class)时创建的Session。在下面的代码中,我更改了这一点,以便除了关闭admin ResourceResolver之外,还在会话中调用logout,并且我还整合了一些重复:

private List<String>  rollOutPages(Collection<Page> pages) throws Exception
{
        ResourceResolver adminResourceResolver;
        List<String> rolledOutPages = new ArrayList<>();
        try {
            adminResourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
            for (Page page : pages) {
                final Collection<LiveRelationship> liveRelationships = relationShipManager.getLiveRelationships(page, null, null, false);
                for (LiveRelationship relationship : liveRelationships) {
                    rolloutManager.rollout(adminResourceResolver, relationship, false);
                    rolledOutPages.add(relationship.getTargetPath());
                }

            }
            Session session;
            try{
                session = adminResourceResolver.adaptTo(Session.class);
                session.save();
            }
            finally{
                session.logout();
            }
            return rolledOutPages;
        }
        catch (WCMException e) {
            LOG.error("Unable to create administrative resource resolver", e);
            throw new AbortException("Exception during rollout of pages",e.getMessage());
        }
        catch (Exception e)
        {
            LOG.error("Exception during rollout of pages", e);
            throw new AbortException("Exception during rollout of pages",e.getMessage());
        }
        finally {
            if(adminResourceResolver != null && adminResourceResolver.isLive()){
                adminResourceResolver.close();
            }
        }

    }

如果您遵循DRY原则,代码/故障排除将更容易 - 不要重复自己。我在你的例子中看到了很多重复的代码。