Thane Plummer
Thane Plummer

Reputation: 10288

How do I resolve the "Node Out Of Date" exception in SenseNet?

Under various circumstances, the same actions will result in a Node Out of Date Exception. Here are two separate instances that caused this exception:

  1. When I update a field on a specific Content item (Order), other Content is created (Invoice) when Order.Save() is called. When I do the update one item at a time everything works fine. However, when I iterate through the list I get a NodeIsOutOfDateException. Putting a delay in the loop -- Sleep(1000) -- makes no difference.
  2. I create new Content and then create child items below it, saving both the parent and the child after each child item is created. When this process completes, I try to move the parent and get the NodeIsOutOfDateException.

What causes this exception and what is the best practice to avoid it?

Upvotes: 1

Views: 113

Answers (1)

Thane Plummer
Thane Plummer

Reputation: 10288

Nodes in SenseNet have a finite lifespan in the Content Repository, even though the Node object you operate on in your source code can hang around as long as you like. You cannot make a global Node variable, load it and perform Repository operations on it without any concern for time.

As a rule, when a new Node comes into existence, either by loading or creating, it is best to sequentially do all your updates to the fields and Save the updated Node as soon as possible.

To resolve the "Node Is Out of Date" exception, just reload the Node immediately before the operation that causes the exception. It may be helpful to save the Node.Id integer variable to facilitate loading.

   var parent = Node.LoadNode(Order.StoragePath);
   Order order = new Order(parent);
   // Assign a bunch of fields then save the order.
   order.Save();

   // Do more operations -- calculate invoices, send emails, etc.

   // order Node is now out of date. You must reload it to 
   // perform Repository operations.
   var orderNotOutOfDate = Node.LoadNode(order.Id) as Order;
   var archiveFolder = GetArchiveFolder();
   order.MoveTo(archiveFolder);   // MoveTo fails if Node is out of date.

Upvotes: 3

Related Questions