How to tell an entity know what session loaded it

16 views
Skip to first unread message

Graham Bunce

unread,
Jun 18, 2011, 10:54:45 AM6/18/11
to nhusers
Hi,

I'm updating a framework we have around NHibernate and we're moving it
from a pattern based around Open Session In View (and we only use one
session per thread for all access) to one that allows us to open and
maintain multiple sessions per thread. We use this session in an
"ambient" manner, i.e. we do not pass it around our business logic but
use it via a Registry pattern - we have a standard class that knows
where to get the current session.

Our problem is that we have some standard business entities that when
methods are called (e.g. "MoveTo(x)" ) they go off to NH, load some
data, do some work then perform the function. This works fine when we
have a single session to worry about. Now we potentially have many
sessions active on the same thread, our business logic doesn't know
which session is active at any one time. So session (A) and session
(B) might have been initiated by our service layer, it then calls
logic on our entity, but the entity itself does a call to NH ... which
session does it load data into? It doesn't know how it got loaded in
the first place so when it starts to do work it can hit issues such as
detached entites and lazy loading, because objects it thinks are in
it's session are actually loaded in the other session.

It's a bit complicated, but the scenario is valid for our situation.

So far, we've thought of simply passing the session into the method
and, although this is an easy option, I was hoping there was some way
I could hook into NHibernate events or it's internal object map to
record some kind of state information so that the business entity can
get a reference to the exact session that loaded it, and it uses the
same session reference for its own internal work.

Any suggestions?

Many thanks

Mohamed Meligy

unread,
Jun 18, 2011, 11:06:24 AM6/18/11
to nhu...@googlegroups.com
If all you are worried about is saving, you might check things like session.Merge.

If this is about retrieving, have you considered depending on second level caching for this? You can make all sessions look at the same 2nd level cache and save some DB hits when the lookup is done. but in the cache not in DB.

In fact, sounds like there is something wrong in the design, from the idea that something creates multiple sessions for something else that it doesn't know which is the current session. At first glance I tend to think maybe you need to work on improving this design itself, but then I remember that you know best about your context and with just that amount of info, nobody can really ague that part correctly.
So, just a hint to *if you think is possible / applicable, and have not done already* look at a way to passing active session as dependency to the things that use it / depend on it.

Regards,

 

Mohamed Meligy
Readify | Senior Developer

M:+61 451 835006 | W: readify.net

Description: Description: Description: Description: rss_16  Description: Description: Description: Description: cid:image003.png@01CAF81D.6A076510  Description: Description: Description: Description: cid:image005.png@01CAF81D.6A076510




--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


Graham Bunce

unread,
Jun 18, 2011, 11:16:12 AM6/18/11
to nhusers
Mohammed,

Thanks for the reply.We have thought of registering a session as the
"active" one so that lower level entites use this. It will work, much
like it will work if we pass the current session into the entity
method. Unfortunately it depends on the developer remembering to
register a session as active - if at all possible, i'd like to make
this invisible to the person using our data access framework that
handles NH interactions, hence wondering if our framework can do some
kind of self-registering mechanism based on the internals of NH.

Fabio Maulo

unread,
Jun 19, 2011, 5:48:38 PM6/19/11
to nhu...@googlegroups.com

Fabio Maulo

unread,
Jun 19, 2011, 5:49:27 PM6/19/11
to nhu...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages