[Cosmo-dev] Re: Hibernate Session in Cosmo
Randy Letness
randy at osafoundation.org
Tue Mar 20 08:01:30 PST 2007
Nicola Piccinini wrote:
>> But It should be a better way. In fact I can't find in code an
>> explicit binding of resources to an Hibernate Sessions when the
>> RPCService is used and I wonder how all that can be automatically
>> carried out (Spring Framework magic, I suppose).
>
> the OpenSessionInViewFilter configured in web.xml is the answer, right?
> Sorry but I'm a Spring newbie.
Right. Cosmo relies on lazy-loading,which requires the session to be
kept open for the duration of the request. This means a new session is
opened, and bound to the Spring transaction manager at the start of the
request, and closed and unbound at the end. This is essentially what
the OpenSessionInViewFilter does. You can also do this by mimicking the
filter. Here is an example you can use in a junit test:
protected void setUp() throws Exception
{
SessionFactory sessionFactory = (SessionFactory)
ctx.getBean("sessionFactory");
session = SessionFactoryUtils.getSession(sessionFactory, true);
TransactionSynchronizationManager.bindResource(sessionFactory,
new SessionHolder(session));
}
protected void tearDown() throws Exception
{
SessionFactory sessionFactory = (SessionFactory)
ctx.getBean("sessionFactory");
SessionHolder holder = (SessionHolder)
TransactionSynchronizationManager.getResource(sessionFactory);
Session s = holder.getSession();
s.close();
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.releaseSession(s, sessionFactory);
}
-Randy
More information about the cosmo-dev
mailing list