I use lazy session:
http://jfromaniello.blogspot.com/2011/03/effective-nhibernate-session-management.html
In the begin request i put a Lazy<ISession>( () => { var s sf.OpenSession();
s.BeginTransaction(); return s; } ) in the request context.
In the end of the request i check if the value was created, and if it was,
commit and close.
So I Open ISession/ITransaction in the first attemp to access it.
No actionfilter for transaction and no opening session when the request
doesn't need.
It is the best way I have seen so far :)
2011/3/31 rhartzog <[email protected]>
> I am set up for session per web request, and every single tiny thing
> gets a session. Did I set something up wrong or is this normal? In
> nhprof I can see 8 session created for a home page which doesn't even
> call for a session. Things like /Scripts/jquery.js, /Content/
> image1.jpg, favicon.ico all get a session.
>
> In global.asax:
> public static ISessionFactory SessionFactory =
> CreateSessionFactory();
> public MvcApplication()
> {
> BeginRequest += MvcApplication_BeginRequest;
> EndRequest += MvcApplication_EndRequest;
> }
>
> void MvcApplication_BeginRequest(object sender, EventArgs e)
> {
> if (HttpContext.Current.Items["CurrentSession"] == null)
> {
> HttpContext.Current.Items["CurrentSession"] =
> SessionFactory.OpenSession();
> }
> }
>
> void MvcApplication_EndRequest(object sender, EventArgs e)
> {
> var session =
> (ISession)HttpContext.Current.Items["CurrentSession"];
>
> if (session != null)
> {
> session.Dispose();
> }
> }
>
> IoC container returns session as it's needed:
>
>
> kernel.Bind<ISession>().ToConstant((ISession)HttpContext.Current.Items["CurrentSession"]);
>
> I put a screenshot of nhprof screen up showing all of the spawned
> sessions, you can see it here (https://picasaweb.google.com/rhartzog/
> NhSetup?authkey=Gv1sRgCJ23-Pqi_djZlwE#5590318316646382866). Any help
> would be appreciated.
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.