On 15.12.10 17:30, "sam lee" wrote:
>when I do,
>
>final ResourceResolver resourceResolver =
>resolverFactory.getAdministrativeResourceResolver(null);
>final Session session = resourceResolver.adaptTo(Session.class);
>
>
>do I need to call session.logout();?
Yes, if you create the session / resource resolver (which is what the
ResourceResolverFactory is doing), you should log out when you no longer
need it. When you only use the ResourceResolver, you should call close()
on it (it will in turn call session.logout() if it is based on a JCR
session).
This is different than for the implicit resourceResolver you get during
normal request processing. Sling manages closing of that one.
>maybe
>
>Session session = null;
>try {
> resolver = resolverFactory.getAdministrativeResourceResolver(null);
> session = resolver.adaptTo(Session.class);
> //spawn thread.. pass session..etc
>
>} catch (...) {
>} finally {
>if (session != null && session.isLive()) {
>session.logout();
>}
>}
Yes, except you don't need the isLive() check (logout() does not throw an
exception if its called twice).
And regarding thread spawning...
>If I spawn threads and pass session, I'll have to pass call back too.
>session.logout() in finally block will cause problem with the spawned
>threads.
JCR Sessions are not thread-safe!
If you have different threads with a need for an admin session, then each
one should create its own session. Also, I would suggest to clean up i.e.
close sessions as soon as possible. Creating a session is cheap.
Regards,
Alex
--
Alexander Klimetschek
Developer // Adobe (Day) // Berlin - Basel