|
From: <jue...@we...> - 2004-08-20 23:23:32
|
I was just about to point exactly that out :-)
=20
Juergen
=20
________________________________
Von: spr...@li... im Auftrag =
von Colin Sampaleanu
Gesendet: Sa 21.08.2004 00:33
An: spr...@li...
Betreff: Re: [Springframework-developer] HibernateInterceptor
Actually, to correct myself, it looks like SessionFactoryUtils does take
care of things.
If the session has been pre-bound with no tx existing (via
OpenSessionInViewFilter classic mode, or HibernateInterceptor), then you
start a new transaction, then you do a SessionFactoryUtils.getSession
(which everything goes through), this piece of code will ensure that the
session is still registered for transaction synchronizaiton after the =
fact:
SessionHolder sessionHolder =3D (SessionHolder)
TransactionSynchronizationManager.getResource(sessionFactory);
if (sessionHolder !=3D null) {
if
(TransactionSynchronizationManager.isSynchronizationActive()) {
if (allowSynchronization &&
!sessionHolder.isSynchronizedWithTransaction()) {
logger.debug("Registering Spring transaction
synchronization for existing Hibernate session");
=20
TransactionSynchronizationManager.registerSynchronization(
new
SpringSessionSynchronization(sessionHolder, sessionFactory,
jdbcExceptionTranslator, false));
sessionHolder.setSynchronizedWithTransaction(true);
}
return sessionHolder.getSession();
}
Now this is only going to happen when some code actually calls
getSession(), so the timing will not be exactly the same as if you used
TransactionInterceptor followed by HibernateInterceptor, but the results
in usage should be the same for all intents and purposes...
Regards,
Colin
Colin Sampaleanu wrote:
> If you use the new mode of the OpenSessionInViewFilter where there are
> multiple sessions, but close is deferred, then in fact there is no
> existing session yet.
>
> If you use the old mode, with just one session, then it's probably not
> the end of the world. The tx sync ensures a flush, and also a callback
> into Hibernate's SessionImplementor.afterTransactionCompletion()
> callback method. With the default flushmode setting, adding back in
> the HibernateInterceptor to force a flush would not do it, since it
> would find the session already on the thread and would not flush on
> coming back, on that basis. You could force a flush with a FLUSH_EAGER
> setting, but then if you had multiple wrapped invocaitons, you'd flush
> on coming back from every one, not just the outer.
>
> I may be missing something; Juergen feel free to comment if the above
> is not accurate.
>
> Regards,
> Colin
>
>
> March, Andres wrote:
>
>> How does this apply to OpenSessionInViewFilter though? The session =
is
>> always opened before a transaction begins.
>>
>>=20
>>
>>> -----Original Message-----
>>> From: spr...@li...
>>> [mailto:spr...@li...] On
>>> =20
>>
>> Behalf
>>=20
>>
>>> Of Colin Sampaleanu
>>> Sent: Friday, August 20, 2004 11:11 AM
>>> To: spr...@li...
>>> Subject: [Springframework-developer] HibernateInterceptor
>>>
>>> Juergen,
>>>
>>> If the HibernateInterceptor is currently applied before the
>>> TransactionInterceptor instead of after it, the invoke method still
>>> ensures the SessionHolder is bound to the thread, but no
>>> =20
>>
>> synchronization
>>=20
>>
>>> can be registered of course. In this case, all you get is a logged
>>> logger.debug("Using new session for Hibernate
>>> =20
>>
>> interceptor");
>>=20
>>
>>> Do you think it's perhaps worth being a bit more explicit and
>>> =20
>>
>> mentioning
>>=20
>>
>>> that no transaction currently exists? Most of the time, unless
>>> =20
>>
>> somebody
>>=20
>>
>>> really wants to use HibernateInterceptor to create sessions outside =
of
>>> =20
>>
>> a
>>=20
>>
>>> transaction, this will be a config error, and people would be better
>>> =20
>>
>> of
>>=20
>>
>>> changing the ordering so that the session is created after the
>>> transaction, and can be synchronized...
>>>
>>> Colin
>>
-------------------------------------------------------
SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
_______________________________________________
Springframework-developer mailing list
Spr...@li...
https://lists.sourceforge.net/lists/listinfo/springframework-developer
|