-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: How to Put NHibernate session outside ASP.NET
PostPosted: Thu May 25, 2006 11:18 pm 
Newbie

Joined: Thu May 25, 2006 11:05 pm
Posts: 5
Hi.

I just want to know if it is possible to put away NHibernate session outside ASP.NET context? Like putting it in in DAO context or at least in BIZ.
I've read a lot forum and article, but most of their pattern usually HttpContext
that directly binds NHibernate session in UI(ASP .NET) level.

Actually, we had a project in JAVA that implement Hibernate and manage the session outside the web appplication. we implemented it in a context of EJB(equivalent is COM+ in MS), So our web application knows nothing about the Hibernate session.It resides in DAO level and I want to do it in .NET the same way we did in JAVA.

So again, is it possible to implement it that way? Or the session can't operate without being
stored in HTTP Context in ASP.NET?

Thanks and I hope you could answer my queries.

Regards,
Eugene


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 12:24 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
i've done exactly what you are asking, although I'm not completely satisfied with the final solution. I have a singleton in my DAO layer that wraps the ISessionFactory for NH. Then I've created an AbstractDAO that acts as a facade and uses the singleton for access to the NH session. Then I implement DAOs as appropriate that extend the AbstractDAO.

in my ASP.NET pages I simply create a new instance to the apropriate DAO and use it to create, read, update, delete, etc...

the disadvantage i see is that i have to manually open and close sessions (my DAO has an overloaded constructor that allows me to specific whether or not to open a session). that isn't so bad.

however, i also have designed a system that uses a single large DAO that is composed into a System.Web.UI.Page class that all of my pages inherit from. This makes it _really_ ease to use the facade I've created. opening ov the session happens at each context_start() and closing the session happens at each context_end().

the individual DAOs follow OO theory in a more formal way, but the single DAO is _really_ easy. did i mention how easy it was? :)

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 1:29 am 
Newbie

Joined: Thu May 25, 2006 11:05 pm
Posts: 5
devonl wrote:
i've done exactly what you are asking, although I'm not completely satisfied with the final solution. I have a singleton in my DAO layer that wraps the ISessionFactory for NH. Then I've created an AbstractDAO that acts as a facade and uses the singleton for access to the NH session. Then I implement DAOs as appropriate that extend the AbstractDAO.

in my ASP.NET pages I simply create a new instance to the apropriate DAO and use it to create, read, update, delete, etc...

the disadvantage i see is that i have to manually open and close sessions (my DAO has an overloaded constructor that allows me to specific whether or not to open a session). that isn't so bad.

however, i also have designed a system that uses a single large DAO that is composed into a System.Web.UI.Page class that all of my pages inherit from. This makes it _really_ ease to use the facade I've created. opening ov the session happens at each context_start() and closing the session happens at each context_end().

the individual DAOs follow OO theory in a more formal way, but the single DAO is _really_ easy. did i mention how easy it was? :)

-devon


Thanks for the info and tips devon.

By the way I did not mention on previous post that I'm currently testing NHibernate session. sorry for that. I implemented also a single ton class for Sesssion Factory that holds the static session. then I have an abstract DAO BaseDAO(Create,Delete,Update,List) that suppose to be inherited by my DAOs like UserDAO,GroupDAO,AuditDAO class. Then I have Biz a classes that calls DAO(i.e. UserBIz calls UserDAO etc.). The main purpose of my Biz layer is to perform and control transaction that will calls multiple DAO by wrapping it one COM+ Transaction.(i.e. UserBiz calls UserDAO.Create() and AuditDAO.Create() method)
See illustration below:

-Biz Class-->
<BeginTransaction>
-->DAO1 Class --Inhetrit--> <BaseDAO> --GetSession--> <SessionFac>
-->DAO2 Class --Inhetrit--> <BaseDAO> --GetSession--> <SessionFac>
-->DAOn class. --Inhetrit--> <BaseDAO> --GetSession--> <SessionFac>
<CommitTransaction>

Since I have an open session, everytime there's a call in DAO method it checks if there is an open session in Session Factory, if there's an open then get it from static variable Session else open a new one.

The problem is this: if I encountered an error like deleting a record with a foreign key to another table or any database access error, the ASP.Net page does not refresh, the error retains even if I changed it to another page.

thanks :-)
Eugene


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 5:57 am 
Newbie

Joined: Thu May 25, 2006 11:05 pm
Posts: 5
Is there anyone here could explain why asp.net page could not reconnect or reuse the current session when an error occurs in db?

Is this because the thread are the same with asp.net and session?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 11:36 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
your design is similar to one i've completed in the past, watch out for marshalling problems. also, i think you are running into another problem which i will explain in a second.

to answer your last question, i think you might be running into a basic tenent of Hibernate/NHibernate when one of your transactions fails you have to abandon all changes. from the Hibernate in Action book:

"the Session has to be immediately closed and discarded (not reused) when an exception occurs."

so, you see, you have to close the session when one of your transaction fails. there is no way around it. no you could still session.Close() and then session.Open() tp continue on.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 28, 2006 9:46 pm 
Newbie

Joined: Thu May 25, 2006 11:05 pm
Posts: 5
devonl wrote:
your design is similar to one i've completed in the past, watch out for marshalling problems. also, i think you are running into another problem which i will explain in a second.

to answer your last question, i think you might be running into a basic tenent of Hibernate/NHibernate when one of your transactions fails you have to abandon all changes. from the Hibernate in Action book:

"the Session has to be immediately closed and discarded (not reused) when an exception occurs."

so, you see, you have to close the session when one of your transaction fails. there is no way around it. no you could still session.Close() and then session.Open() tp continue on.

-devon


Devon,
What's up!
Yes, I follow what you mean, I implemnted a session.close in catch exception in my AbstractDAO and it works and solve my problem. I really appreciate your contribution here.

Anyway, I'll have to continue my testing on this to ensure that it's working effectively and efficiently. If i'm successful on this, I'll recommend this as our .NET framework in our company because most of employees here
are familiar in Hibernate in JAVA. Now, It's time for them to learn new things in .NET way. :-)

ciao,
Eugene


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.