Flush NHibernate, still allowing transaction rollback

I am trying to use NHibernate with deprecated objects that do not map to NHibernate. Sometimes this means that I need to manually clear the NHibernate data to the database so that I don't get foreign key exceptions when I try to connect legacy objects with objects displayed by NHibernate.

The problem occurs when this occurs in a transaction, which then needs to be discarded. Data discarded from NHibernate is not rolled back.

What can I do about it?

UPDATE

Still curious how to do this - I do not believe that none of the answers given in this question affect this problem. I need to call Flush (). The question is, how to reset the rollback data?

+5
source share
3 answers

check this: force request without flash / commit

I seemed to have the same problem, I would hide, and then I would roll back, but some data would remain in the database. However, there were some parts in my code that would cause a commit that cannot be undone. Consider the code fragment of the accepted answers as the correct use of transactions, flushes, rollbacks and commits and note that this template can be expanded ...

in one unit of work (i.e., we consider a request in a web application as a single unit of work, and everything that happens in this request exists in one transaction that onEndRequest commits):

  • you call the _sessionFactory.OpenSession(), _session.BeginTransaction(), _session.CommitTransaction()and _session.CloseSession()only once.

  • _session.Flush() _session.RollBackTransaction() , , Flush() Commit . Flush, , .

  • , . NHibernate (http://www.nhprof.com/Learn/Alerts/DoNotUseImplicitTransactions), , ,

  • , , .

  • , ; "" , . .NET SQL Server, , , ADO.NET.

1 4 NHibernate, 1.2.

+4

tolism7 .

  • using . Dispose() - transaction Dispose 'd .
  • throw - ex, (. , : " .NET Framework : throw ex; ." )

.

public void CommitChanges()
{
    using (var transaction = Session.BeginTransaction())  // <-- open scope
        try
        {
            // do something
            transaction.Commit();
        }
        catch (HibernateException)
        {
            transaction.Rollback();
            _session.Close();
            _session.Dispose();

            throw;  // <-- this way the stacktrace stays intact!
        }
}

A VB.NET .

+3

NHibernate Session.Flush() transaction.Commit(), session.flush() .

Commit() , .

public static void CommitChanges()
{
    ITransaction transaction = Session.BeginTransaction();

    try
    {
        transaction.Commit();
    }
    catch (HibernateException ex)
    {
        transaction.Rollback();
        //close and dispose session here
        throw ex;
    }
    finally
    {
        transaction.Dispose();
    }
}

, flush() commit() , NHibernate. , transaction.Commit(), AdoTransaction, NHibernate, Commit(), .

, , .

. Commit() session.Flush() , , .

, , , , transaction.commit() Session.Flush() , .

+2
source

All Articles