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

  • Home
  • SEARCH
  • 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 851089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:22:24+00:00 2026-05-15T07:22:24+00:00

How do i roll back changes to an attached object in nhibernate? We have

  • 0

How do i roll back changes to an attached object in nhibernate? We have a validation before update/save (example simplified)..

var setting = Get("key")
setting.Value = "helo" //for whatever reason, this particular 
                       //setting cannot have its value saved to the database
...
Verify(setting);       //throws
base.Update(setting);

but since the object is attached, any changes already happened in the session, even if the validation throws, and never reaches Update. What is the proper way to handle this?

  • 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-15T07:22:25+00:00Added an answer on May 15, 2026 at 7:22 am

    This is most probably because the surrounding transaction commits, even an exception is thrown. Or you catches the exception later on and hide the error from the surrounding transaction. The update could be performed by NH whenever it flushes the session, for instance before executing queries. Committing always synchronizes the memory state to the database.

    Update is only needed for instances which are not in the session yet (but in the database). Everything in the session is synchronized to the database, any any point in time, at the latest when committing.

    One of the advantages of NH is what is called “persistence ignorance”. This means, after instances are loaded to memory, you logic doesn’t care about persistency anymore. You’re using your classes like any other, at the end everything will be persisted (or rolled back) in an atomic way.

    So what does this all mean for your code?

    • Write your code as if there weren’t NHibernate or persistency. This will also enhance maintainability and testability.
    • What you are doing in memory is definitive. You don’t have a second “store” step. Don’t do things you don’t want to do.
    • Your memory needs to be as consistent as the database. Your logic is always based on memory state (eg. calculations). Inconsistent memory will break consistency of the application. So you shouldn’t commit when you have data in memory you don’t want in the database.
    • Commit is all or nothing. Either you are happy with your changes or you roll back everything.
    • Exception handling is tricky. It’s not always easy to decide if you can carry on with the transaction.
    • Changing the state of the entity is responsibility of your business logic. NHibernate will not “reset” a single instance. (There is a Refresh, but it will not work here and you shouldn’t use it anyway). So if you want to reset a value, you need to write business logic which resets the value.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just did a delete from table How do I roll back or undelete
When you roll out changes to a live web site, how do you go
We have a website that uses #include file command to roll info into some
Any recommendations for a javascript form validation library. I could try and roll my
I have a specific changeset that I want to rollback my Development branch to,
I need to do transactions (begin, commit or rollback), locks (select for update). How
I understand the mantra of don't roll your own when it comes to site
I want the log to roll over as long as the application is running,
Does Java Connection.close rollback into a finally block?. I know .Net SqlConnection.close does it.
I tried using "cap rollback" but I get the following error: $ cap rollback

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.