You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Fight Ice <ri...@hotmail.com> on 2012/05/30 09:03:28 UTC

About per-thread service

I have read the source code of HibernateCoreModule.java and HibernateSessionManagerImpl.java .
The HibernateSessionManager service is per-thread, but I didn't find any code for concurrency(like using ThreadLocal). How Tapestry make HibernateSessionManager service to be per-thread.
 		 	   		  

RE: About per-thread service

Posted by Lance Java <la...@googlemail.com>.
Let me answer some different questions:

Q. If I make multiple hibernate calls in a single request, how many
hibernate sessions were created?
A. 1

Q. Will a hibernate session be created for a request that does not use
hibernate?
A. No

Q. Since there is only 1 instance of Session in the tapestry registry, how
can it be thread safe?
A. It's a proxy which delegates to a lazy loaded session on a thread local

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

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


RE: About per-thread service

Posted by Fight Ice <ri...@hotmail.com>.
The @Scope(ScopeConstants.PERTHREAD) makes the service per-thread, and finally the SessionFactory.openSession() will be called(SessionFactory is thread safe).
One session for one thread(request)?
 		 	   		  

Re: About per-thread service

Posted by Lance Java <la...@googlemail.com>.
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-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org