The code that does the magic is below. The HibernateSessionManager is
configured as a per thread service. The Session is configured as a
PropertyShadowBuilder which lazily calls
HibernateSessionManager.getSession() on the per thread service.

/**
 * The session manager manages sessions on a per-thread/per-request basis.
Any active transaction will be rolled
 * back at {@linkplain org.apache.tapestry5.ioc.Registry#cleanupThread()
thread cleanup time}.  The thread is
 * cleaned up automatically in a Tapestry web application.
 */
@Scope(ScopeConstants.PERTHREAD)
public static HibernateSessionManager
buildHibernateSessionManager(HibernateSessionSource sessionSource,
                                                                  
PerthreadManager perthreadManager)
{
    HibernateSessionManagerImpl service = new
HibernateSessionManagerImpl(sessionSource);

    perthreadManager.addThreadCleanupListener(service);

    return service;
}

public static Session buildSession(HibernateSessionManager sessionManager,
                                   PropertyShadowBuilder
propertyShadowBuilder)
{
    // Here's the thing: the tapestry.hibernate.Session class doesn't have
to be per-thread,
    // since
    // it will invoke getSession() on the HibernateSessionManager service
(which is per-thread).
    // On
    // first invocation per request,
    // this forces the HSM into existence (which creates the session and
begins the
    // transaction).
    // Thus we don't actually create
    // a session until we first try to access it, then the session continues
to exist for the
    // rest
    // of the request.

    return propertyShadowBuilder.build(sessionManager, "session",
Session.class);
}


--
View this message in context: 
http://tapestry.1045711.n5.nabble.com/About-per-thread-service-tp5713515p5713518.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to