PDA

View Full Version : How to user Nhibernate lazyload in windows application?


Garfield
05-10-2007, 05:38 AM
Now, I use osiv in my windows application.
In the App.config:

<system.web>
<httpModules>
<add name="Spring"
type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
<add name="OpenSessionInView"
type="Spring.Data.NHibernate.Support.OpenSessionInViewMo dule, Spring.Data.NHibernate"/>
</httpModules>
</system.web>

In the application form:

private void Form1_Load(object sender, EventArgs e)
{
osiv.Open();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
osiv.Close();
}


But it still doesn't work. When I use lazyload, It throw an "...no session." exception. Are there any problems of my code? Is it a right way to use the Nhibernate's lazyload in the windows application? If is not, how can I do?

BTW:
When it run at osiv.Open(), the console's debug infomation:

2007/05/10 12:55:35:453 [DEBUG] Spring.Context.Support.ContextRegistry - Returning context 'Spring.Context.Support.XmlApplicationContext' registered under name 'spring.root'.
2007/05/10 12:55:35:453 [DEBUG] Spring.Objects.Factory.Support.DefaultListableObje ctFactory - Returning cached instance of singleton object 'sessionFactory'.
2007/05/10 12:55:35:453 [DEBUG] Spring.Objects.Factory.Support.DefaultListableObje ctFactory - Object with name 'sessionFactory' is a factory object.
2007/05/10 12:55:35:453 [DEBUG] Spring.Data.NHibernate.Support.OpenSessionInViewMo dule - Creating LazySessionHolder in OpenSessionInView
2007/05/10 12:55:35:453 [DEBUG] Spring.Data.NHibernate.Support.OpenSessionInViewMo dule+LazySessionHolder - Created LazySessionHolder
2007/05/10 12:55:35:468 [DEBUG] Spring.Transaction.Support.TransactionSynchronizat ionManager - Bound value [Spring.Data.NHibernate.Support.OpenSessionInViewMo dule+LazySessionHolder] for key [NHibernate.Impl.SessionFactoryImpl] to thread []


But when it implement a dao code, the console's debug information:

2007/05/10 13:32:12:875 [DEBUG] Spring.Data.NHibernate.SessionFactoryUtils - Opening Hibernate Session
2007/05/10 13:32:14:812 [DEBUG] Spring.Data.NHibernate.HibernateAccessor - Eagerly flushing Hibernate session
2007/05/10 13:32:14:875 [DEBUG] Spring.Data.NHibernate.SessionFactoryUtils - Closing Hibernate Session

The session is closed.

Erich Eichinger
05-10-2007, 05:50 AM
Hi,

You say "Windows Application", but configured the <system.web> section. To avoid confusion: Are we talking about a Windows Forms or a Web application?

When does the call to your failing DAO method occur? Maybe you can post a more verbose version of your log + your spring context+objects configuration?

cheers,
Erich

Garfield
05-10-2007, 06:14 AM
hi,
My program is windows form.
Actually, I don't know how to config the App.config for the osiv. Is there any example?
I load an object from the dao. When I use a lazyload property in the manager layer, it throws a "...no session" exception.

My Dao.xml:

<objects xmlns="http://www.springframework.net" xmlns:db="http://www.springframework.net/database">

<!-- Property placeholder configurer for database settings -->
<object type="Spring.Objects.Factory.Config.PropertyPlaceholderC onfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>

<!-- Database and NHibernate Configuration
<db:dbProvider id="DbProvider"
provider="SqlServer-1.1"
connectionString="Data Source=${db.datasource};Database=${db.database};Us er ID=${db.user};Password=${db.password};Trusted_Conn ection=False"/>
-->
<db:dbProvider id="DbProvider"
provider="SqlServer-1.1"
connectionString="Data Source=${db.datasource};User ID=${db.user};Password=${db.password}"/>

<object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>AlarmCheck.Dao.SpringNHB</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<!--
<entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
-->
<entry key="hibernate.dialect" value="NHibernate.Dialect.OracleDialect"/>
<entry key="hibernate.connection.driver_class" value="NHibernate.Driver.OracleClientDriver"/>
</dictionary>
</property>
</object>

<object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager , Spring.Data.NHibernate12">
<property name="DbProvider" ref="DbProvider"/>
<property name="SessionFactory" ref="SessionFactory"/>
</object>
<!--
<object id="SessionScopeAdvice" type="Framework.Net2.Core.Domain.Dao.SpringNHB.SessionSc opeAdvice, Framework.Net2.Core.Domain.Dao.SpringNHB">
</object>
-->
<!-- Data Access Objects -->
<object id="IAlarmCheckPeriodDao" type="AlarmCheck.Dao.SpringNHB.AlarmCheckPeriodDao, AlarmCheck.Dao.SpringNHB">
<property name="SessionFactory" ref="SessionFactory"/>
</object>

<object id="IAlarmCheckProjectDao" type="AlarmCheck.Dao.SpringNHB.AlarmCheckProjectDao, AlarmCheck.Dao.SpringNHB">
<property name="SessionFactory" ref="SessionFactory"/>
</object>

<object id="ICheckStationDao" type="AlarmCheck.Dao.SpringNHB.CheckStationDao, AlarmCheck.Dao.SpringNHB">
<property name="SessionFactory" ref="SessionFactory"/>
</object>

<object id="IAlarmLevelDao" type="AlarmCheck.Dao.SpringNHB.AlarmLevelDao, AlarmCheck.Dao.SpringNHB">
<property name="SessionFactory" ref="SessionFactory"/>
</object>

<object id="ICheckStationAlarmConditonDao" type="AlarmCheck.Dao.SpringNHB.CheckStationAlarmConditon Dao, AlarmCheck.Dao.SpringNHB">
<property name="SessionFactory" ref="SessionFactory"/>
</object>

<object id="IAlarmCheckDataDao" type="AlarmCheck.Dao.SpringNHB.AlarmCheckDataDao, AlarmCheck.Dao.SpringNHB">
<property name="SessionFactory" ref="SessionFactory"/>
</object>
</objects>

Erich Eichinger
05-10-2007, 10:20 AM
Hi,

OSIV has originally been implemented to support webapplications. I started refactoring this class but unfortunately couldn't finish it yet.

Here's my (private) opinion on how to approach a WinForms/Hibernate application:

You need to ensure, that your calls to/from NHibernate are grouped in a "ConversationScope". This means that e.g. a Click on a Button opens a Session, does some DB-stuff (ideally within a transaction) and finally closes the session again.

Steinar, another forum user has created a "SessionScope" attribute here (http://forum.springframework.net/showthread.php?p=6285#post6285) that exactly allows you to do that.

hope this helps,
Erich

Garfield
05-11-2007, 01:33 PM
I know why the osiv doesn't work in my win form app, becourse I open it in the main thread, but I use the dao in the background thread. Now, I open the osiv when the background thread is started, that' OK.

Thanks.