Steve,
My use case is very simple, I think this is the most common case when I need hibernate/jboss integraiton.
1. I have one hibernate service.
2. I use it from both servlet & ejb.
3. From either context, I want just call getSession() when I need hibernate session, work with it and forget about any cleanup code.
4. I want than hibernate session what I used in servlet was flushed and closed when I return from servlet,
respectively, I want the session I get being in ejb was flushed and closed when CMT commits.
If this usecase fits well into existing architecture, maybe you can explain it here or in wiki?
My experience is based on existing wiki page, and what I get:
Lets suppose I have my hibernate service deployed, say, as jboss.har:service=MyHibernateService.
Hibernate session is described as unit of work which is in my understanding (usually)
transaction-wide for ejb and servlet's service()-wide for servlet.
I have ejb's and servlets which want to use it. Basically, I need to get hibernate session
in servlet or ejb. In both cases I just get session like this:
Session session = SessionContext.getSession("java:/hibernate/MySessionFactory");
providing service deployed with
<mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=MyHibernateService">
<attribute name="SessionFactoryName">java:/hibernate/MySessionFactory</attribute>
In servlet I want session was opened before entering servlet (or maybe lazy when I call getSession())
and flushed and closed when I left servlet.
In ejb I want session to be flushed and closed when transaction commited.
so the idea is just getSession() when I need it and forget about explicit flushing and closing.
How should I configure filter & interceptor?
according to wiki:
<container-name>Hibernate Stateless SessionBean</container-name>
<interceptor service="jboss.har:service=MyHibernateService" scope="transaction">org.jboss.hibernate.session.EjbInterceptor</interceptor>
and
<filter-name>Hibernate Session Filter</filter-name>
<filter-class>org.jboss.hibernate.session.FilterInterceptor</filter-class>
<init-param>
<param-name>service</param-name>
<param-value>jboss.har:service=MyHibernateService</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>thread</param-value>
</init-param>
respectively.
I need access to concrete hibernate service (jboss.har:service=MyHibernateService) identified by
its SessionFactoryName (java:/hibernate/MySessionFactory)
After adding filter & interceptor to my application config I code in ejb something like this:
session = getSession("java:/hibernate/MySessionFactory");
entity = session.load(...);
session.save(entity)
and I expect session will be flushed and closed when my CMT commited.
But I get session with scope THREAD as it was described in servlet filter - because it's the same
factory name and it was overriden.
Well, I need both servlet & ejb get session from this factory (java:/hibernate/MySessionFactory), but session management should be different in terms of cleanup (you're saying this as well).
steve wrote:
No, the jndi name of the session-factory in this context is really just a convenience namespace for handles to sessions. The scope is just an implementation detail of the interceptors, describing how you want the resultant session managed internally (really it only describes cleanup semantics).
Right, it's all about cleanup.
steve wrote:
It is totally intended that sessions from a given session factory be bound with only a single scope for a given call stack.
Please correct me if I'm wrong. But it seems that I have exactly one session factory bound to one hibernate service. (session factory name defined in hibernate service xml). I get sessions from
this factory from different contexts (basically servlets&ejbs) where I expext different cleanup
behaviour, according to scope defined in interceptors. But you allow define only one scope(=cleanup strategy) per session factory(=hibernate service).
steve wrote:
But really, what is a valid use case for having a session bound twice with both thread and transaction scope? Or even two sessions from the same session factory with different scopes? I can't think of any; if you can give me *valid* use cases where this is desirable, then I'll rethink this.
I do not understand this. Session factory produces sessions.
Session factory itself doesn't care of how session will be closed.
Closing algorithm defined by context.
Then, why can't I have two sessions from one factory which being used in different contexts have different cleaning strategy??
Thanks,