I am examining the sample code, and notice that the ASP.net sample is using SessionManager HttpHandler which is not mentioned in the PDF manual.

From my understanding from the code sample and this thread

http://forum.x-tensive.com/viewtopic.php?f=29&t=5760&p=13878&hilit=sessionmanager#p13878

  1. SessionManager saves me from typing by creating a new Session and TransactioScope for me automagically.

  2. I can rollback by setting SessionManager.Current.HasErrors = true;

  3. The only work I have to do is to set SessionManager.DomainBuilder when initializing my domain, and hook up and handler in web.config.

Beyond these, does SessionManager has other capabilities that I haven't discover?

This thread was imported from our support forum. The original discussion may contain more detailed answer. Original topic by ccchai.

asked Apr 03 '10 at 04:43

Editor's gravatar image

Editor
46156156157


One Answer:

Alex (Xtensive) wrote:

Yes, exactly. There are no other capabilities - the only its purpose is to provide Session & transaction for each web request and allow to rollback it in case of error.

Ah, forgot... HasError is automatically set to true if error occurs on execution of a web request, although you can manually modify it as well.


ness wrote:

Don't want to start new topic, so...

Help me, please :)

Now I'm trying to test capabilities of DO because I want to use it on real commercial projects instead of EF.

And I don't understand some behaviour of DO.

I have asp.net website and SessionManager is used.

And some code in Page_Load

protected void Page_Load(object sender, EventArgs e)
    {
......
    ChatSessions.Create(refSite);
......

    }

Code for ChatSessions.Create

public static ChatSession Create(Site site)
{
using (var tr = Transaction.Open(TransactionOpenMode.New))
{
    var cs = new ChatSession();

    cs.Operator = Operators.GetFreeOperator(refSite);

    cs.DtStart = DateTime.Now;
    cs.Site = site;
    cs.Status = ChatSessionStatus.New;

    tr.Complete();

    return cs;
}
}

Trouble happens on this line:

cs.Operator = Operators.GetFreeOperator(refSite);

I have LINQ query in Operators.GetFreeOperator(), and when DO executes this query, error occurs.

In SQL statements in exception message I see something like this:

INSERT INTO ChatSession (..) VALUES (..);
SELECT ().....FROM Operator .....

But INSERT fails, because Operator field can't be NULL.

Why DO executes INSERT before SELECT? Transaction isn't commited yet, and Operators.GetFreeOperator only selects data, no changes to database.

All methods aren't makred with [Transactional].

Thanks a lot.

P.S. Sorry for my English.


Dmitri Maximov (Xtensive) wrote:

Hello,

There are 2 approaches to cope with such a situation:

  1. Load necessary data before creating ChatSession. Here is the updated code. Firstly, you execute query and after that you instantiate and set ChatSession properties.

    ... var operator = Operators.GetFreeOperator(refSite); var cs = new ChatSession(); cs.Operator = operator; ...

  2. Another one requires some changes to the domain model design:

First of all, move ChatSession creation logic into its constructor.

The second step is to block persisting of ChatSession instance until it is fully initialized. This can be done with Session.Pin method, e.g.:

public ChatSession(site, refSite, etc.) {

  // By this line we indicate that this instance should not be persisted while Pin is active
  using(Session.Pin(this)) {

     cs.Operator = Operators.GetFreeOperator(refSite);
     cs.DtStart = DateTime.Now;
     cs.Site = site;
     cs.Status = ChatSessionStatus.New;
  }
}

Hope that helps.


ness wrote:

Ok, thanks.

I can use you first approach for now.

May be I don't understand something, but how transaction works in such situation?

We have:

public static ChatSession Create(Site site, Operator oper)
{
using (var tr = Transaction.Open(TransactionOpenMode.New))
{
   var cs = new ChatSession();

   cs.Operator = oper;/// no queries, object already exists

   cs.DtStart = DateTime.Now;
   cs.Site = site;
   cs.Status = ChatSessionStatus.New;

  var xx = Something.GetSingle(); // getting something with query to database
  .....//some code

   if (isOk)
 {
     tr.Complete();
     return cs;
  }

   return null;
}
}

I understand, that I should check isOk before creating object, but in general — cs will be stored to database if isOk = false?

Thanks for you answer.

Привет Екатеринбургу из Киева :)


Dmitri Maximov (Xtensive) wrote:

In case you don't call tr.Complete() method, the transaction will be rolled back with all changes that were made to the database. In your case this means that the row which corresponds to newly created ChatSession object will disappear from the corresponding table.

Привет Екатеринбургу из Киева :) Взаимно! =)

answered Apr 03 '10 at 06:27

Editor's gravatar image

Editor
46156156157

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

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×574

Asked: Apr 03 '10 at 04:43

Seen: 3,260 times

Last updated: Apr 03 '10 at 04:43

powered by OSQA