|
From: Colin S. <col...@ex...> - 2004-08-05 12:08:16
|
Juergen, I was going to do this myself. I am a little bit worried about making HibernateAccessor (parent of HibernateInterceptor) BeanFactoryAware though. That would also make HibernateTemplate dependent on BeanFactory where it wasn't before. I suppose the contract could be declared specifically in the JavaDoc, that the BeanFactory is only needed or used to obtain the entity interceptor, when the interceptor is provided by name. The classes are already InitializingBean aware, in any case. Alternately, it could be left as is, with the proviso that people must use method replacement to get a prototype entity interceptor in there if needed. But I don't see this as very clean. To move from HibernateTransactionManager to JtaTransactionManager, with identical behaviour, somebody really has to use HibernateInterceptor. It makes sense to have HibernateInterceptor be consistently configured the same way. I guess the final option is to roll back the changes to HibernateTransactionManager and ask people to use lookup method injection, with the javadocs for the getEntityInterceptor() method in the two classes being very explicit about that being a template method to be replaced if needed to return a prototype. Colin Colin Sampaleanu wrote: > Juergen, > > That's good. HibernateInterceptor also needs a similar method, for use > with JTATransactionManager... > > Colin > > jürgen höller [werk3AT] wrote: > >> I've just added a setEntityInterceptorBeanName method to >> HibernateTransactionManager, and made it BeanFactoryAware for the >> purpose of resolving such a bean name. So you can now either register >> a specific reference or a bean name; the latter will typically be >> used for prototypes (i.e. one interceptor instance per session). >> >> Juergen >> >> >> ________________________________ >> >> Von: Colin Sampaleanu [mailto:col...@ex...] >> Gesendet: Mo 02.08.2004 20:52 >> An: spr...@li... >> Cc: jürgen höller [werk3AT] >> Betreff: Re: [Springframework-user] HibernateTransactionManager & >> session level interceptor >> >> >> >> Feifei, >> >> I have though of a way you can handle your use case actually, and that's >> to use Spring 1.1's lookup method injection support: >> http://www.springframework.org/docs/reference/beans.html#d0e876 >> to modify HibernateTransactionManager so that its getEntityInterceptor() >> method returns a new prototype (non-singleton) Hibernate entity >> interceptor instance on each invocation. This would work when using >> HibernateTransactionManager. >> >> When using JTATransactionManager, what you would want to do is pair >> JTATransactionManager with HibernateInterceptor, and for >> HibernateInterceptor, override its getEntityInterceptor() method in the >> same fashion. >> >> Juergen, if you're reading this, it's probably worth modifying both of >> the above classes so that they support having a prototype (session >> specific) hibernate interceptor applied out of the box. They could >> potentially take a bean name for a hibernate entity interceptor, which >> if it existed would result in the classes doing a getBean on the >> container to get the specified (theoretically) prototype bean. They >> would have to be made BeanFactoryAware, but they are already Spring >> specific anyways, having the InitializingBean interface. >> >> Colin >> >> >> Colin Sampaleanu wrote: >> >> >> >>> I strongly recommend against even trying to set the interceptor from >>> the template. Even with JTATransactionManager, only the first use of a >>> template (with allowCreate=true of course) will end up creating a new >>> session and binding it to the current thread. It is also associated >>> with the transaction (same as if you had used >>> HibernetateTransactionManager) from that point on, and any subsequent >>> template use within that transaction (with the same SessionFactory >>> associated with the template) will not end up creating a new session. >>> So realistically, any use of Spring's HibernateTemplate class with >>> Spring's transaction support does not allow you to know ahead of time >>> when using a template if in fact you will get a new session or reuse >>> an existing one. And this is intentional, since the whole point is to >>> share the same session for the whole transaction, so you don't have to >>> worry about managing it, and accidentally having some objects in a >>> graph coming from one session, and some from another. >>> >>> >>> >>> >>> Feifei Li wrote: >>> >>> >>> >>>> Colin: >>>> Thanks for your reply. What I want to ask is : is >>>> there any way I can set thread-unsafe interceptor on >>>> session level if I use HibernateTransactionManager? >>>> Currently I try to apply Audit Info design pattern to >>>> my project(http://www.hibernate.org/48.html). In this >>>> pattern, it uses entity interceptor(AuditInterceptor) >>>> to set property; this interceptor is sesion-level >>>> interceptor(thread-unsafe), I can't set it on session >>>> factory level. My current approach is set this >>>> interceptor on HibernateTemplate(based on Spring java >>>> doc), but it is not work if I use >>>> HibernateTransactionManager(seems it work on >>>> JTATransactionManager because no session is pre bound; >>>> and when SessionFactoryUtil create session, it will >>>> use HibernateTemplate interceptor). Is this any way I >>>> can solve this issue? >>>> >>>> Thanks >>>> Feifei >>>> --- Colin Sampaleanu <col...@ex...> wrote: >>>> >>>> >>>> >>>> >>>> >>>>> FeiFei, >>>>> >>>>> I don't really know what you're asking here, to tell >>>>> you the truth. The hibernate entity interceptor handling is pretty >>>>> clearly documented. Yes, if HibernateTransactionManager is in use, >>>>> it will >>>>> ensure that a Hibernate Session is bound to the current thread >>>>> when invocation goes through it, so by the time that >>>>> hibernateTemplate >>>>> comes into the picture, no new Hibernate Session will have to be >>>>> created, so any hibernate entity interceptor specified in the >>>>> template is not going to ever be applied... >>>>> >>>>> Colin >>>>> >>>>> >>>>> Feifei Li wrote: >>>>> >>>>> >>>>> >>>>> >>>>> >>>>>> Hi: >>>>>> According to Spring's Java document, Spring >>>>>> >>>>> >>>>> supports >>>>> >>>>> >>>>> >>>>> >>>>>> hibernate entity interceptor on session factory >>>>>> >>>>> >>>>> level >>>>> >>>>> >>>>> >>>>> >>>>>> and session level: If you config Interceptor on >>>>>> LocalSessionFactoryBean, you set interceptor on >>>>>> session factory level; If you call >>>>>> setEntityInterceptor on HibernateTemplate, you set >>>>>> interceptor on session level. It is pretty clear >>>>>> >>>>> >>>>> but I >>>>> >>>>> >>>>> >>>>> >>>>>> am really confused by HibernateTransactionManager. >>>>>> Based on my understand If I set interceptor on >>>>>> HibernateTransactionManager, I set interceptor on >>>>>> session factory level(correct me if I am wrong). so >>>>>> >>>>> >>>>> if >>>>> >>>>> >>>>> >>>>> >>>>>> I use HibernateTransactionManger, I have no way to >>>>>> >>>>> >>>>> use >>>>> >>>>> >>>>> >>>>> >>>>>> session level interceptor. because each time when I >>>>>> get new transaction, HibernateTransactionManger >>>>>> >>>>> >>>>> will >>>>> >>>>> >>>>> >>>>> >>>>>> get new hibernate session(based on itself >>>>>> configuration) and bound it to current thread. so >>>>>> >>>>> >>>>> even >>>>> >>>>> >>>>> >>>>> >>>>>> I set interceptor on HibernateTemplate level, it >>>>>> actually useless, because session already been >>>>>> >>>>> >>>>> bound >>>>> >>>>> >>>>> >>>>> >>>>>> to thread. >>>>>> >>>>>> Did I misunderstand sth or it is a Spring's bug or >>>>>> limitation? I will be appreciate for any input, >>>>>> >>>>> >>>>> this >>>>> >>>>> >>>>> >>>>> >>>>>> issue really confuse me. >>>>>> >>>>>> Thanks >>>>>> Feifei >>>>>> |