using System; using System.IO; using System.Linq; using Db4objects.Db4o; using Db4objects.Db4o.Linq; namespace Db4OSeries { public class Program { private const string FilePath = "database.db4o"; internal static void Main(string[] args) { File.Delete(FilePath); using (var db = OpenClient()) { db.Store(new SimpleObject() {Name = "Object-1"}); new Demo1(db).Run(); } Console.ReadKey(); } private static IObjectContainer OpenClient() { var cfg = Db4oEmbedded.NewConfiguration(); return Db4oEmbedded.OpenFile(cfg, FilePath); } } internal class Demo1 { private readonly IObjectContainer db; public Demo1(IObjectContainer db) { this.db = db; } public void Run() { RestoreObject(); // Interleaving 1 { var toUpdate = GetTheUpdateObject(); InOtherThreadThisRanThrought( () => { var toUpdateT2 = GetTheUpdateObject(); toUpdateT2.Amount = toUpdate.Amount*2; toUpdateT2.Price = toUpdate.Price/2; db.Store(toUpdateT2); }); var sum = toUpdate.Price; sum = sum*toUpdate.Amount; toUpdate.Sum = sum; Console.Out.WriteLine("Sum is=" + toUpdate.Sum + " Expected is 8"); db.Store(toUpdate); } RestoreObject(); // Interleaving 2 { var toUpdate = GetTheUpdateObject(); var sum = toUpdate.Price; InOtherThreadThisRanThrought( () => { var toUpdateT2 = GetTheUpdateObject(); toUpdateT2.Amount = toUpdate.Amount*2; toUpdateT2.Price = toUpdate.Price/2; db.Store(toUpdateT2); }); sum = sum*toUpdate.Amount; toUpdate.Sum = sum; Console.Out.WriteLine("Sum is=" + toUpdate.Sum + " Expected is 8"); db.Store(toUpdate); } } private void RestoreObject() { var toUpdate = GetTheUpdateObject(); toUpdate.Amount = 2; toUpdate.Price = 4; } private void InOtherThreadThisRanThrought(Action action) { action(); } private SimpleObject GetTheUpdateObject() { return (from SimpleObject o in db where o.Name == "Object-1" select o).First(); } } }