Sid
Sid

Reputation: 355

nHibernate Session-Per-Request Delete and commit before session ends

I'm using a gridview in c# and am trying to delete a row. I am using nHibernate with session-per-request. My problem is when I delete and perform a row command I can delete the record in db but when I try to reload the datasource, because the delete hasn't committed yet I seem to be fetching the original data within the same session.

Can someone tell me what the best strategy is to handle this scenario?

Many thanks! Sid

Upvotes: 0

Views: 445

Answers (1)

cbp
cbp

Reputation: 25628

You should run two transactions. In WebForms, your code might look like this:

using (var transaction = Session.BeginTransaction())
{
    var foo = Session.Load<Foo>(fooId);
    Session.Delete(foo);

    transaction.Commit();
}

using (var transaction = Session.BeginTransaction())
{
    gridView.DataSource = Session.List<Foo>(fooId);
    gridView.DataBind()

    transaction.Commit();
}

Like Radim Kohler says, if you were doing this the more modern RESTful way in MVC, you should have the client make two calls to your API - a Delete call, then a List call. The principal is the same though - you need two transactions.

I'm not quite sure why NHibernate doesn't AutoFlush in your scenario, but if you are not opening a transaction at all, then you may find NHibernate's AutoFlush feature does not work as it should.

Upvotes: 1

Related Questions