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 7668209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:18:15+00:00 2026-05-31T15:18:15+00:00

I’m thinking about making my own IUnitOfWork implementation for an NHibernate persistence layer. It

  • 0

I’m thinking about making my own IUnitOfWork implementation for an NHibernate persistence layer.

It seems that the right way to do this would be to have the ISession and the ITransaction instantiated in the constructor, and then disposed in the destructor or the Dispose() method.

Of course, if someone invokes the Save() method, then the ISession would be flushed and the ITransaction would be complete, so after calling Save(), there would not be a valid open transaction to Save() again… unless I committed the first transaction and immediately opened another, new transaction. But is this a good idea?

Design-wise, it makes sense to do one commit operation, but I will not necessarily have control of the code and other developers may be less disciplined about following the UnitOfWork pattern.

Do I lose/ gain anything by trying to make the UnitOfWork tolerant to multiple transactions per session? Should I just check for an open transaction and throw an exception if it’s already been committed, rather than making a new transaction?

  • 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-31T15:18:17+00:00Added an answer on May 31, 2026 at 3:18 pm

    To answer the first question: yes it is possible to have multiple transactions in one session.

    Is is a good idea? It depends.

    The problem is that changing data in the first transaction would be committed, while it is not sure if the whole unit of work (session) gets committed at the end. When you get, let’s say, a StaleObjectException in a later transaction, you already had some data committed. Note that this kind of exception makes your session unusable and you had to destroy it anyway. Then it is hard to start over and try again.

    I would say, it works well under these circumstances:

    • It is a UI application
    • Changes are only flushed in the last transaction.

    UI Application

    Errors are handled by the user interactively. This means that the user can see what’s actually stored in a case of error and repeats the changes he made.

    Changes are only flushed in the last transaction

    The session as implemented by NH only flushes changes at the end or “when necessary”. So it would be possible to keep changes in memory until the session gets committed. The problem is that NH needs to flush the session before every query, which is hard to control. It can be turned off, which leads to side effects. When writing simple transactions, you may have it under control. In a complex system it’s virtually impossible to make sure that nothing is going wrong.

    The Simple Way ™

    I wrote the persistence layer of a quite large client-server system. In such a system, you don’t have a user handling errors directly. You need to handle the errors in the system and return control to the client in a consistent state.

    I simplified the whole transaction handling to an absolute minimum, in order to make it stable and “idiot proof”. I have always a session and a transaction created together and it gets either committed or not.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.