Thank you for your help. The exception I was talking about ("open data
reader...") hasn't occurred any more so far.
If I understood correctly: session = proxy and any changes are
automatically tracked and applied when I call Commit() on tx. If so, if I
do session.Dispose() what is happening with current (not committed)
transaction (because of exception for example)? Is rollback automatically?
What is happening with session when NHibernateException is called? I feel
that it is broken in such case and I can't reuse it any more (for populate
a view for example). How to manage such cases?
Best regards.
Wlodzimierz
Below my changes. I hope that it will be useful for someone. :)
Web.config:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
> <session-factory>
> <property
> name="current_session_context_class">NHibernate.Context.WebSessionContext,
> NHibernate</property>
> ....
>
Global.asax:
protected void Application_BeginRequest(object sender, EventArgs e)
> {
> if
> (!CurrentSessionContext.HasBind(NHibernateSessionManager.SessionFactory))
>
> CurrentSessionContext.Bind(NHibernateSessionManager.SessionFactory.OpenSession());
> else
> System.Diagnostics.Debug.WriteLine("CurrentSessionContext
> has bind!");
> }
>
> protected void Application_EndRequest(object sender, EventArgs e)
> {
> var session =
> CurrentSessionContext.Unbind(NHibernateSessionManager.SessionFactory);
>
> if (session != null)
> session.Dispose();
> }
>
Session Factory manager:
public class NHibernateSessionManager
> {
> private static object SyncObject = new object();
> private static ISessionFactory _SessionFactory = null;
>
> public static ISessionFactory SessionFactory
> {
> get
> {
> lock (SyncObject)
> {
> if (_SessionFactory == null)
> {
> Configuration configuration = new
> Configuration().Configure();
> _SessionFactory =
> configuration.BuildSessionFactory();
> }
>
> return _SessionFactory;
> }
> }
> }
>
> public static ISession OpenSession()
> {
> return SessionFactory.OpenSession();
> }
>
> public static ISession CurrentSession
> {
> get
> {
> return SessionFactory.GetCurrentSession();
> }
> }
> }
>
And finally some example code:
{
> ...
> var session = NHibernateSessionManager.CurrentSession;
> using (var tx = session.BeginTransaction())
> {
> var obj = session.Load<SomeClass>();
> obj.someProperty = 'someValue';
> tx.Commit();
> }
> ...
> }
>
W dniu niedziela, 10 marca 2013 09:34:51 UTC+1 użytkownik Oskar Berggren
napisał:
>
> ManagedWebSessionContext is deprecated, use WebSessionContext instead.
>
> session.Dispose() is more true-to-form than close (always dispose
> disposable objects).
>
> It's _completely_ useless and misleading to call session.Update() on
> an object that you just loaded from the session. The object is already
> known and tracked by the session.
>
> The transaction will be automatically rollbacked on dispose unless it
> has been committed, so no need to duplicate the built-in try-catch
> logic of the using statement.
>
> ISessionFactory is thread safe, so no need to lock around call to
> OpenSession().
>
> But I don't see any obvious problem that would cause the mentioned
> exception in the code you have posted.
>
> /Oskar
>
>
> 2013/3/7 Włodzimierz <[email protected] <javascript:>>:
> >
> > Hello,
> >
> > I am trying to work with managed sessions in my web application (MVC4).
> I
> > have read tutorials but I feel I am doing something wrong. I don't know
> why
> > but I feel that I get the same session for many request because I often
> get
> > "Open data reader..." exception. The problem is that I get this
> exception
> > even if I am sure that I have not used any command, especially in
> > BeginTransaction().... Commit() block. There is some example function
> below:
> >
> >
> >>
> >> ISession session = NHibernateSessionManager.CurrentSession;
> >>
> >> using (ITransaction tx = session.BeginTransaction())
> >> {
> >> var account = session.Query<....................>
> >>
> >> try
> >> {
> >> var q = session.Load<..........................>
> >>
> >> q.someProperty = 'a';
> >>
> >> session.Update(q);
> >> tx.Commit();
> >> }
> >> catch (Exception e)
> >> {
> >> tx.Rollback();
> >> }
> >> }
> >
> >
> >
> > My nhibernate web.config configuration file:
> >
> >> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
> >> <session-factory>
> >> <property
> >>
> name="current_session_context_class">NHibernate.Context.ManagedWebSessionContext,
>
>
> >> NHibernate</property>
> >> <property
> >> name="dialect">NHibernate.Dialect.PostgreSQL82Dialect</property>
> >> <property
> >>
> name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
>
>
> >> <property name="connection.connection_string">...</property>
> >> <property
> >>
> name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory,
>
>
> >> NHibernate</property>
> >> <property name="show_sql">true</property>
> >> ...
> >> </session-factory>
> >> </hibernate-configuration>
> >
> >
> > As you can see, I have set ManagedWebSessionContext to get one session
> per
> > request. I assume that I that it will be new session per each every
> request,
> > by using this code in global.asax.cs:
> >
> >> protected void Application_BeginRequest(object sender,
> EventArgs
> >> e)
> >> {
> >> ManagedWebSessionContext.Bind(
> >> HttpContext.Current,
> >> NHibernateSessionManager.SessionFactory.OpenSession());
> >> }
> >>
> >> protected void Application_EndRequest(object sender, EventArgs
> e)
> >> {
> >> ISession session = ManagedWebSessionContext.Unbind(
> >> HttpContext.Current,
> >> NHibernateSessionManager.SessionFactory);
> >>
> >> if (session != null)
> >> {
> >> if (session.Transaction != null &&
> >> session.Transaction.IsActive)
> >> session.Transaction.Rollback();
> >>
> >> session.Close();
> >> }
> >> }
> >
> >
> > It uses NHibernateSessionManager class listed below to manage sessions:
> >
> >> public class NHibernateSessionManager
> >> {
> >> private static object SyncObject = new object();
> >> private static ISessionFactory _SessionFactory = null;
> >>
> >> internal static ISessionFactory SessionFactory
> >> {
> >> get
> >> {
> >> lock (SyncObject)
> >> {
> >> if (_SessionFactory == null)
> >> {
> >> Configuration configuration = new
> >> Configuration().Configure();
> >> _SessionFactory =
> >> configuration.BuildSessionFactory();
> >> }
> >>
> >> return _SessionFactory;
> >> }
> >> }
> >> }
> >>
> >> public static ISession OpenSession()
> >> {
> >> lock (SyncObject)
> >> {
> >> return SessionFactory.OpenSession();
> >> }
> >> }
> >>
> >> public static ISession CurrentSession
> >> {
> >> get
> >> {
> >> return SessionFactory.GetCurrentSession();
> >> }
> >> }
> >> }
> >
> >
> > Any help would be appreciated. If you have some tested and working code,
> > such help would be great as well.
> >
> > Best regards.
> >
> >
> >
> >
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups
> > "nhusers" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an
> > email to [email protected] <javascript:>.
> > To post to this group, send email to [email protected]<javascript:>.
>
> > Visit this group at http://groups.google.com/group/nhusers?hl=en.
> > For more options, visit https://groups.google.com/groups/opt_out.
> >
> >
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.