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.  [ 2 posts ] 
Author Message
 Post subject: Architecture : where to begin/close transaction and session?
PostPosted: Fri Oct 06, 2006 2:41 am 
Newbie

Joined: Fri Oct 06, 2006 2:06 am
Posts: 1
Hello, I'm working on a project with a premade solution containing different projects representing layers :
Business entities, data layer, business logic layer and presentation layer ( at least it's how I understand them ). We have also some class which supposely help us to use nhibernate easily ...

One recommendation made was to use transactions in the business logic layer ( because the data layer wouldn't know if we were in a transaction context or not ), the other was to get the current session and close it in the data layer. Also we use one method / query ( well sometimes it's not exactly like this ) in the data layer.
Is that the right "way" to use nhibernate? Doing any kind of transaction like this is a bit painful ( often some errors, so we have to find workaround...)

I've the feeling that we don't use nhibernate like it should, but I don't have really any experience with mapping o/r tools or plainly "architecture design"/real po knowledge.
In the FAQ, the exemple is "getting session, starting transaction, works, closing transaction, closing session" but should it be in the data or business logic layer? Is this exemple accurate for a 3-tier architecture?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 08, 2006 4:30 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
If you are doing a Web app, typically you have one NHibernate session-per-request. NHibernate has its own unit of work, so when you flush the session at the end of each request, NHibernate will synchronize changes made to your entities with the DB.
This is typical usage, but NHibernate lets you control this even more granularly if necessary.
If this is a win forms app, i think you'd keep session as a singleton per thread (correct me if i'm wrong!)

You can get an NHibernate session inside a custom HttpModule if a web app. The session factory is constructed once per application startup since it is expensive procedure.

As far as when to flush/commit the transaction to the db, it depends on your app requirements. I prefer to control when things get persisted and don't use NHibernate's transparent-write-behind functionality, but I know many do. The NHibernate ISession API makes dealing with transactions really simple.

Read Hibernate in Action to get deeper into transaction/session management ideas.

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.