In your MVC samples, in a controller you call Session.Demand() to get a session, then execute queries and work with entities without creating a transaction. Why? Are they automatically created by SessionManager?

asked Dec 29 '10 at 22:34

ara's gravatar image

ara
395828791

edited Jan 04 '11 at 16:02

Sergey's gravatar image

Sergey
123339


One Answer:

Yes, exactly. A bit more precise info:

  • Session is open, when you invoke Session.Demand() for the first time in the current HttpContext. If Domain isn't build @ that time (or its previous build has failed), it will be built.
  • "Virtual" transaction (i.e. DO object) is created immediately, but actual transaction is only started with the first DB query. (SessionOptions.AutoShortenTransaction does this, but this properly works only in v4.4).
  • If no error occurs, transaction is committed. Otherwise it's rolled back. You can also control this using SessionManager properties.
  • AFAIK, published version has a minor bug in SessionManager: when domain build fails, it can't rebuild it on the subsequent web request. It's already fixed in repo, so the nearest update will resolve this issue. Currently you have to restart web app to get it rebuilt.

answered Dec 30 '10 at 01:42

Alex%20Yakunin's gravatar image

Alex Yakunin
29714412

A tiny addition: it's better to use the following bases for your controllers and views with MVC:

public class SessionBoundController : Controller
{
    private Xtensive.Orm.Session session;

    public HttpSessionStateBase SessionState {
        get { return base.Session; }
    }

    public new Xtensive.Orm.Session Session {
        get {
            if (session==null)
                session = Xtensive.Orm.Session.Demand();
            return session;
        }
    }
}
(Dec 30 '10 at 01:45) Alex Yakunin Alex%20Yakunin's gravatar image

And view:

public abstract class SessionBoundWebViewPage<TModel> : WebViewPage<TModel>
{
    private Xtensive.Orm.Session session;

    public HttpSessionStateBase SessionState {
        get { return base.Session; }
    }

    public new Xtensive.Orm.Session Session {
        get {
            if (session==null)
                session = Xtensive.Orm.Session.Demand();
            return session;
        }
    }
}
(Dec 30 '10 at 01:45) Alex Yakunin Alex%20Yakunin's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA