PDA

View Full Version : How To Configure A Session


jnapier
08-08-2006, 06:53 AM
Does anybody know how to configure an NHibernate Session to have the correct Flush Mode and use an Interceptor?

Also, I am having a tough time understanding the scope of an Nhibernate Session with the HibernateTemplate. Basically I'm not seeing any lazy loading benefits because it seems like a session is opened and closed for every operation. Is there a way to configure the scope of a session?

Session-Per-Request
Session-Per-Application transaction

If using a NHibernate with an Asp.Net application, I think there will be the need for an HttpModule that will be able to close the session.

.ben
08-08-2006, 11:39 AM
Hibernate on its own, does not take care of that. It's up to the end user to do that. However one fo the aims of the Spring Integration project is to manage that for you I believe. If you search around on the nhibernate site / forum you'll find enough information.

swalters
08-08-2006, 01:11 PM
To handle transactions, I wrap all of my DAO calls within a Spring managed transaction. These are configured per use case so that any proxies needed for that use case are loaded.

To handle the interceptor, I extended LocalSessionFactoryWithInterceptor and overrode the PostProcessConfiguration:

class LocalSessionFactoryWithInterceptor:LocalSessionFac toryObject
{
public LocalSessionFactoryWithInterceptor():base()
{}

protected override void PostProcessConfiguration(Configuration config)
{
base.PostProcessConfiguration(config);
config.Interceptor = new IsDirtyInterceptor();
}

}
Then I use this in my configuration for the Session Factory.

<object id="SessionFactory" type="Singletree.Core.DaoHibernate.Extensions.LocalSessi onFactoryWithInterceptor, Singletree.Core.DaoHibernate">
-Shane

jnapier
08-08-2006, 03:55 PM
.ben, I'm talking about configuring the session with Spring. I am familiar with configuring the flush mode and interceptor directly with the NHibernate API. I see that the HibernateAccessor base class has properties for TemplateFlushMode and Interceptor. I was thinking that this may be the way to specify these values for a session, but then I ran across the OpenSessionInViewModule class and it looks like the sessions are created in a completely different way than with the HibernateTemplate and there is no way to specify an IInterceptor or Flush Mode. Now I am thoroughly confused.

Hopefully here is a solid example coming soon.

jnapier
08-08-2006, 04:29 PM
swalters, your method would work for configuring an IInterceptor at the factory level. The same IInterceptor will be used for every session. This may be what you want, but not always. There may be times when you want each session to have its own instance of an IInterceptor. I’m actually looking to create a new Interceptor for each session. It looks like the setting the IInterceptor through the HibernateAccessor may be the route to go, but I’m not sure.

Transaction Scope and Session scope may not necessarily be the same. So I'm not sure if I understand why you are talking about transaction scope. May initial question was about session scope not transaction scope. I’m having a problem with lazy initialization similar to http://forum.springframework.net/showthread.php?t=641.

Although, I do need help with transaction scope as well. Is the transaction attribute the only way to demarcate a transaction? Or can I do it programmatically also?

I have this configured, but I'm not really sure of the implications.

<!-- NHIBERNATE TRANSACTION MANAGER -->
<object id="hibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager , Spring.Data.NHibernate">
<property name="DbProvider" ref="DbProvider"/>
<property name="sessionFactory" ref="SessionFactory"/>
</object>

<!--NHIBERNATE TRANSACTION INTERCEPTOR: based on attribute [Transaction()] -->
<object id="transactionInterceptor" type="Spring.Transaction.Interceptor.TransactionIntercep tor, Spring.Data">
<property name="TransactionManager" ref="hibernateTransactionManager"/>
<!-- note do not have converter from string to this property type registered -->
<property name="TransactionAttributeSource">
<object type="Spring.Transaction.Interceptor.AttributesTransacti onAttributeSource, Spring.Data"/>
</property>
</object>

I'm just using this from the example in the integration tests project. Maybe I need to track down the java documentation. Hopefully that can provide some insight.

Any other pointers would be highly appreciated. I think other users that have not worked with Spring in Java before may find themselves in the same situation I am in.