Skip to content

Instantly share code, notes, and snippets.

@ChrisMissal
Created April 23, 2010 15:34
Show Gist options
  • Save ChrisMissal/376694 to your computer and use it in GitHub Desktop.
Save ChrisMissal/376694 to your computer and use it in GitHub Desktop.
using (var txn = session.BeginTransaction())
{
try
{
session.SaveOrUpdate(entity);
session.Flush(); // <-- is this even necessary? UPDATE... Not necessary, thanks! :)
txn.Commit();
}
catch (HibernateException)
{
txn.Rollback();
throw;
}
}
@hyrmn
Copy link

hyrmn commented Apr 23, 2010

Not necessary. Here's mine:

    private void WithinTransaction(Action action)
    {
        ITransaction transaction = Session.BeginTransaction();
        try
        {
            action();
            transaction.Commit();
        }
        catch (Exception e)
        {
            transaction.Rollback();
            throw;
        }
        finally
        {
            transaction.Dispose();
        }
    }

    public void Save(T entity)
    {
        WithinTransaction(() => Session.SaveOrUpdate(entity));
    }

@ChrisMissal
Copy link
Author

@benhyr thanks!

@hyrmn
Copy link

hyrmn commented Apr 23, 2010

Reposting my tweet here.

I spoke too soon. I believe it may be necessary if you've changed the session.FlushMode (I believe it's FlushMode.Auto by default). Otherwise, you may need to manually do the flush in certain cases (likely not if if you're using GUID or HighLow keys but I'm not positive on those)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment