Re: New tutorial about 'Getting Resources and Properties in Sling'

2010-12-15 Thread Alexander Klimetschek
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






Re: New tutorial about 'Getting Resources and Properties in Sling'

2010-12-15 Thread sam lee
when I do,

final ResourceResolver resourceResolver =
resolverFactory.getAdministrativeResourceResolver(null);
final Session session = resourceResolver.adaptTo(Session.class);


do I need to call session.logout();?

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();
}
}

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.

Or, do I have to session.logout() at all given I get the Session object
through

resolverFactory.getAdministrativeResourceResolver(null).adaptTo(Session.class)
  ??


Thanks.
Sam

On Wed, Dec 15, 2010 at 10:31 AM, Jean-Christophe Kautzmann <
jkaut...@adobe.com> wrote:

> Hi,
>
> this is to let you know that a tutorial about 'Getting Resources and
> Properties in Sling' [1] has been added to the Sling documentation.
>
> Cheers,
> JC
>
> [1]
> http://sling.apache.org/site/getting-resources-and-properties-in-sling.html
>
>


New tutorial about 'Getting Resources and Properties in Sling'

2010-12-15 Thread Jean-Christophe Kautzmann

Hi,

this is to let you know that a tutorial about 'Getting Resources and 
Properties in Sling' [1] has been added to the Sling documentation.


Cheers,
JC

[1] 
http://sling.apache.org/site/getting-resources-and-properties-in-sling.html