Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4584328
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:20:21+00:00 2026-05-21T21:20:21+00:00

I understand that you should always use transactions. I also see that NHibernate.ITransaction implement

  • 0

I understand that you should always use transactions. I also see that NHibernate.ITransaction implement a Dispose, so I need to wrap my ITransaction object in a using statement. So, does that mean that for every non-readonly repository method (i.e. update, edit…) I have to declare:

using (ITransaction _transaction = _session.BeginTransaction(//isolationlevel)) {
  //procedure code here
}

Is there a way that I can wrap this (I am not seeing that I can).

Also, is it best that I wrap both a session.SaveOrUpdate() method and transaction.Commit() in a Try/Catch?

using (_transaction = _session.BeginTransaction(IsolationLevel.ReadCommitted) {
  try {
    _session.SaveOrUpdate(entity);
      try {
        _transaction.Commit();
      catch (//some exception ex) {
        _transaction.RollBack();
      }
  }
  catch (//some exception ex) {
    //log ex
  }
}

Or is there a better way such as having both session and transaction methods in the same try/catch?

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:20:22+00:00Added an answer on May 21, 2026 at 9:20 pm

    The recommended practice when using the ISession directly is this: (you should use a transaction even for reads)

    using(var session = factory.OpenSession())
    using(var tx = session.BeginTransaction())
    {
        // all the code that uses the session goes here
        // use session to load and/or save entity
    }
    

    This is basically creating an unit of work.

    Depending on your operation context ( web request, wcf request ) you might want to have all the operation inside a single unit of work. For web request see this for WCF operation see this.

    Also as Sixto Saez said calling SaveOrUpdate is a smell. In most of the cases you have one of these two cases:

    1 You create a new entity and call session.Save(entity);

    or

    2 You get an entity or more from the session ( with Get/Load or by query ), modify the entity and the changes will be saved by nhibernate on session disposal. ( unless you change the session flush mode but that is not the point ).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a newcomer to Python but I understand that things should not be done
This is a short question. At some point my thread understand that it should
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
I understand that in my unit test I should do BOOST_TEST_MESSAGE(message); instead of std::cout
I'm having trouble with something that I understand should function out of the box
I understand that we need to create MXML file to define a view. Suppose
I understand that some hash tables use buckets, which is a linked list of
I recall that one should always avoid using recursive method calls in Java .
I took a JSP class and we learnt that we should always remove all
I understand that Web Site Projects compile source on-the-fly, and Web Application Projects pre-compile

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.