title: How do I make an in-memory process transactional?
body: <p>I'm very familiar with using a transaction RDBMS, but how would I make sure that changes made to my in-memory data are rolled back if the transaction fails?  What if I'm not even using a database?</p>
  
  <p>Here's a contrived example:</p>
  
  <pre><code>public void TransactionalMethod()
  {
      var items = GetListOfItems();
  
      foreach (var item in items)
      {       
          MethodThatMayThrowException(item);
  
          item.Processed = true;
      }
  }
  </code></pre>
  
  <p>In my example, I might want the changes made to the items in the list to somehow be rolled back, but how can I accomplish this?</p>
  
  <p>I am aware of "software transactional memory" but don't know much about it and it seems fairly experimental.  I'm aware of the concept of "compensatable transactions", too, but that incurs the overhead of writing do/undo code.</p>
  
  <p>Subversion seems to deal with errors updating a working copy by making you run the "cleanup" command.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>UPDATE:</strong><br>
  <a href="https://stackoverflow.com/users/65358/reed-copsey">Reed Copsey</a> offers <a href="https://stackoverflow.com/questions/1461137/how-do-i-make-an-in-memory-process-transactional#1461162">an excellent answer</a>, including:</p>
  
  <blockquote>
    <p>Work on a copy of data, update original on commit.</p>
  </blockquote>
  
  <p>This takes my question one level further - what if an error occurs <strong>during the commit</strong>?  We so often think of the commit as an immediate operation, but in reality it may be making many changes to a lot of data.  What happens if there are unavoidable things like <code>OutOfMemoryException</code>s while the commit is being applied?</p>
  
  <p>On the flipside, if one goes for a rollback option, what happens if there's an exception <strong>during the rollback</strong>?  I understand things like Oracle RDBMS has the concept of rollback segments and UNDO logs and things, but assuming there's no serialisation to disk (where if it isn't serialised to disk it didn't happen, and a crash means you can investigate those logs and recover from it), is this really possible?</p>
  
  <p><strong>UPDATE 2:</strong><br>
  An <a href="https://stackoverflow.com/questions/1461137/how-do-i-make-an-in-memory-process-transactional/#1464584">answer</a> from <a href="https://stackoverflow.com/users/114916/alex">Alex</a> made a good suggestion: namely that one updates a different object, then, the commit phase is simply changing the reference to the current object over to the new object.  He went further to suggest that the object you change is effectively a list of the modified objects.</p>
  
  <p>I understand what he's saying (I think), and I want to make the question more complex as a result:</p>
  
  <p>How, given this scenario, do you deal with locking?  Imagine you have a list of customers:</p>
  
  <pre><code>var customers = new Dictionary&lt;CustomerKey, Customer&gt;();
  </code></pre>
  
  <p>Now, you want to make a change to <strong>some</strong> of those customers, how do you apply those changes without locking and replacing the entire list?  For example:</p>
  
  <pre><code>var customerTx = new Dictionary&lt;CustomerKey, Customer&gt;();
  
  foreach (var customer in customers.Values)  
  {
      var updatedCust = customer.Clone();     
      customerTx.Add(GetKey(updatedCust), updatedCust);
  
      if (CalculateRevenueMightThrowException(customer) &gt;= 10000)
      {
          updatedCust.Preferred = true;
      }
  }
  </code></pre>
  
  <p>How do I commit?  This (Alex's suggestion) will mean locking <strong>all</strong> customers while replacing the list reference:</p>
  
  <pre><code>lock (customers)
  {
      customers = customerTx;
  }
  </code></pre>
  
  <p>Whereas if I loop through, modifying the reference in the original list, it's not atomic,a and falls foul of the "what if it crashes partway through" problem:</p>
  
  <pre><code>foreach (var kvp in customerTx)
  {
      customers[kvp.Key] = kvp.Value;
  }
  </code></pre>