Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

Session has been closed after closing resource resolver object in AEM

I am trying to close the resource resolver that I opened but I got this error “javax.jcr.RepositoryException: This session has been closed when I close resource resolver. Actually I don’t see any problem if you leave the resource resolver as open but I don’t want to leave the resource resolver open in the code.

serviceParam.put(ResourceResolverFactory.SUBSERVICE, "serviceNew");
        ResourceResolver resourceResolver = null;

        try
        {

            resourceResolver = resourceResolverFactory.getServiceResourceResolver(serviceParam);

            final Configuration configuration = configurationManagerFactory.getConfigurationManager(resourceResolver)
                    .getConfiguration(cloudConfigurationType.getServiceName(), services);

            if (null != configuration)
            {
               return configuration.getContentResource().adaptTo(ValueMap.class);
            }

        } catch (LoginException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally
        {
            if (resourceResolver != null && resourceResolver.isLive())
            {
                resourceResolver.close();
            }
        }

Error message:-

org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught SlingException
java.lang.IllegalArgumentException: javax.jcr.RepositoryException: This session has been closed.
    at org.apache.sling.jcr.resource.internal.JcrValueMap.read(JcrValueMap.java:337) [org.apache.sling.jcr.resource:3.0.16]
    at org.apache.sling.jcr.resource.internal.JcrValueMap.get(JcrValueMap.java:101) [org.apache.sling.jcr.resource:3.0.16]

Observing many error messages like below in logs If I dont close a resource resolver in code

 23.12.2020 11:56:30.481 [Apache Sling Resource Resolver Finalizer Thread] INFO o.a.s.r.i.CommonResourceResolverFactoryImpl Unclosed ResourceResolver was created here:
    java.lang.Exception: Opening Stacktrace
    at org.apache.sling.resourceresolver.impl.CommonResourceResolverFactoryImpl$ResolverReference.<init>(CommonResourceResolverFactoryImpl.java:540)

The real problem is, session getting closed as soon as we close resource resolver. Is this an issue with AEM code? or Any other way to handle this case?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, basically the problem is the code. Closing a ResourceResolver by definition closes the session it is based on as well. The trick is to close it after you don't need all the depending objects any more (like the ValueMap for example).

HTH.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...