public void CreateMoreDataWithBackupServer()
        {
            int loops = 1000;
            int j;

            using (ServerClientSession session = new ServerClientSession(s_systemDir, systemHost, 1000, true, inMemoryOnly))
            {
                Man   aMan   = null;
                Woman aWoman = null;
                session.BeginUpdate();
                for (j = 1; j <= loops; j++)
                {
                    aMan = new Man(null, aMan, DateTime.Now);
                    session.Persist(aMan);
                    aWoman = new Woman(aMan, aWoman);
                    session.Persist(aWoman);
                    aMan.m_spouse = new VelocityDb.WeakIOptimizedPersistableReference <VelocityDbSchema.Person>(aWoman);
                    if (j % 1000000 == 0)
                    {
                        Console.WriteLine("Loop # " + j);
                    }
                }
                UInt64 id = aWoman.Id;
                Console.WriteLine("Commit, done Loop # " + j);
                session.FlushUpdates();
                ReadSomeData(); // read some with uncommited cached server data
                session.Commit();
            }
        }
 public void CreateMoreDataWithBackupServer()
 {
   int loops = 1000;
   int j;
   using (ServerClientSession session = new ServerClientSession(s_systemDir, systemHost, 1000, true, inMemoryOnly))
   {
     Man aMan = null;
     Woman aWoman = null;
     session.BeginUpdate();
     for (j = 1; j <= loops; j++)
     {
       aMan = new Man(null, aMan, DateTime.Now);
       session.Persist(aMan);
       aWoman = new Woman(aMan, aWoman);
       session.Persist(aWoman);
       aMan.m_spouse = new VelocityDb.WeakIOptimizedPersistableReference<VelocityDbSchema.Person>(aWoman);
       if (j % 1000000 == 0)
         Console.WriteLine("Loop # " + j);
     }
     UInt64 id = aWoman.Id;
     Console.WriteLine("Commit, done Loop # " + j);
     session.FlushUpdates();
     ReadSomeData(); // read some with uncommited cached server data
     session.Commit();
   }
 }
        public void AppendFile()
        {
            Placement place = new Placement(798, 1, 1, 1, UInt16.MaxValue);

            using (ServerClientSession session = new ServerClientSession(systemDir))
            {
                session.BeginUpdate();
                ObjWithArray a = new ObjWithArray(10);
                a.Persist(place, session);
                session.Commit(); // commit Database 798
            }
            place = new Placement(798, 2, 1, 100, UInt16.MaxValue);
            for (int i = 0; i < 25; i++)
            {
                using (ServerClientSession session = new ServerClientSession(systemDir))
                {
                    //session.SetTraceAllDbActivity();
                    session.BeginUpdate();
                    for (int j = 0; j < 1000; j++)
                    {
                        ObjWithArray a = new ObjWithArray(j * 10);
                        a.Persist(place, session);
                    }
                    session.FlushUpdates();
                    session.FlushUpdatesServers(); // check if this will cause file to be appended
                    Database db = session.NewDatabase(3567);
                    using (ServerClientSession session2 = new ServerClientSession(systemDir))
                    {
                        session2.BeginUpdate();
                        ObjWithArray a = new ObjWithArray(10);
                        a.Persist(place, session2);
                        session2.Commit();
                    }
                    session.Abort(); // appended page space now unused? Need tidy?
                }
            }
        }
 public void AppendFile()
 {
   Placement place = new Placement(798, 1, 1, 1, UInt16.MaxValue);
   using (ServerClientSession session = new ServerClientSession(systemDir))
   {
     session.BeginUpdate();
     ObjWithArray a = new ObjWithArray(10);
     a.Persist(place, session);
     session.Commit(); // commit Database 798
   }
   place = new Placement(798, 2, 1, 100, UInt16.MaxValue);
   for (int i = 0; i < 25; i++)
   {
     using (ServerClientSession session = new ServerClientSession(systemDir))
     {
       //session.SetTraceAllDbActivity();
       session.BeginUpdate();
       for (int j = 0; j < 1000; j++)
       {
         ObjWithArray a = new ObjWithArray(j * 10);
         a.Persist(place, session);
       }
       session.FlushUpdates();
       session.FlushUpdatesServers(); // check if this will cause file to be appended
       Database db = session.NewDatabase(3567);
       using (ServerClientSession session2 = new ServerClientSession(systemDir))
       {
         session2.BeginUpdate();
         ObjWithArray a = new ObjWithArray(10);
         a.Persist(place, session2);
         session2.Commit();
       }
       session.Abort(); // appended page space now unused? Need tidy?
     }
   }
 }
        public void multipleServersInvalid()
        {
            Assert.Throws <InvalidChangeOfDefaultLocationException>(() =>
            {
                using (ServerClientSession session = new ServerClientSession(systemDir))
                {
                    session.SetTraceDbActivity(2);
                    try
                    {
                        DatabaseLocation localLocation = new DatabaseLocation(systemHost, location2Dir, 10000, 20000, session, PageInfo.compressionKind.LZ4, 0);
                        Placement place = new Placement(10000, 2);
                        session.BeginUpdate();
                        session.NewLocation(localLocation);
                        Man aMan     = null;
                        Woman aWoman = null;

                        for (int j = 1; j <= 5; j++)
                        {
                            aMan = new Man(null, aMan, DateTime.UtcNow);
                            aMan.Persist(place, session);
                            aWoman = new Woman(aMan, aWoman);
                            aWoman.Persist(place, session);
                            aMan.m_spouse = new WeakIOptimizedPersistableReference <VelocityDbSchema.Person>(aWoman);
                            if (j % 1000 == 0)
                            {
                                session.FlushUpdates();
                            }
                        }
                        localLocation = new DatabaseLocation(systemHost, systemDir, 20001, 30000, session, PageInfo.compressionKind.None, 0);
                        session.NewLocation(localLocation);
                        place = new Placement(20001);
                        //localDatabase = session.NewDatabase(20001, localLocation);
                        for (int j = 1; j <= 5; j++)
                        {
                            aMan = new Man(null, aMan, DateTime.UtcNow);
                            aMan.Persist(place, session);
                            aWoman = new Woman(aMan, aWoman);
                            aWoman.Persist(place, session);
                            aMan.m_spouse = new WeakIOptimizedPersistableReference <VelocityDbSchema.Person>(aWoman);
                            if (j % 1000 == 0)
                            {
                                session.FlushUpdates();
                            }
                        }
                        DatabaseLocation serverLocation = new DatabaseLocation(systemHost2, location2Dir, 30001, 40000, session, PageInfo.compressionKind.LZ4, 0);
                        session.NewLocation(serverLocation);
                        place = new Placement(30001);
                        for (int j = 1; j <= 5; j++)
                        {
                            aMan = new Man(null, aMan, DateTime.UtcNow);
                            aMan.Persist(place, session);
                            aWoman = new Woman(aMan, aWoman);
                            aWoman.Persist(place, session);
                            aMan.m_spouse = new WeakIOptimizedPersistableReference <VelocityDbSchema.Person>(aWoman);
                            if (j % 1000 == 0)
                            {
                                session.FlushUpdates();
                            }
                        }
                        session.Commit();
                    }
                    finally
                    {
                        //session.Close();
                    }
                }
            });
        }
        [Repeat(3)] // remove when propagtion of optimistic locking flag is done to slave database locations TO DO (issue caused by CopyAllDatabasdesTo that uses pessimistic locking)
        public void multipleServersOK()
        {
            using (SessionNoServer session = new SessionNoServer(systemDir))
            {
                session.BeginRead();
                foreach (DatabaseLocation loc in session.DatabaseLocations)
                {
                    Console.WriteLine(loc.ToStringDetails(session, false));
                }
                session.Commit();
            }

            using (ServerClientSession session = new ServerClientSession(systemDir, systemHost))
            {
                session.SetTraceDbActivity(0);
                DatabaseLocation localLocation = new DatabaseLocation(systemHost, location2Dir, 10000, 20000, session, PageInfo.compressionKind.LZ4, 0);
                Placement        place         = new Placement(10000, 2);
                session.BeginUpdate();
                foreach (DatabaseLocation loc in session.DatabaseLocations)
                {
                    Console.WriteLine(loc.ToStringDetails(session, false));
                }
                Console.WriteLine();
                session.NewLocation(localLocation);
                foreach (DatabaseLocation loc in session.DatabaseLocations)
                {
                    Console.WriteLine(loc.ToStringDetails(session, false));
                }
                Man   aMan   = null;
                Woman aWoman = null;

                for (int j = 1; j <= 5; j++)
                {
                    aMan = new Man(null, aMan, DateTime.UtcNow);
                    aMan.Persist(place, session);
                    aWoman = new Woman(aMan, aWoman);
                    aWoman.Persist(place, session);
                    aMan.m_spouse = new WeakIOptimizedPersistableReference <VelocityDbSchema.Person>(aWoman);
                    if (j % 1000 == 0)
                    {
                        session.FlushUpdates();
                    }
                }
                localLocation = new DatabaseLocation(systemHost2, systemDir, 20001, 30000, session, PageInfo.compressionKind.LZ4, 0);
                session.NewLocation(localLocation);
                foreach (DatabaseLocation loc in session.DatabaseLocations)
                {
                    Console.WriteLine(loc.ToStringDetails(session, false));
                }
                place = new Placement(20001);
                //localDatabase = session.NewDatabase(20001, localLocation);
                for (int j = 1; j <= 5; j++)
                {
                    aMan = new Man(null, aMan, DateTime.UtcNow);
                    aMan.Persist(place, session);
                    aWoman = new Woman(aMan, aWoman);
                    aWoman.Persist(place, session);
                    aMan.m_spouse = new WeakIOptimizedPersistableReference <VelocityDbSchema.Person>(aWoman);
                    if (j % 1000 == 0)
                    {
                        session.FlushUpdates();
                    }
                }
                DatabaseLocation serverLocation = new DatabaseLocation(systemHost2, location2Dir, 30001, 40000, session, PageInfo.compressionKind.LZ4, 0);
                session.NewLocation(serverLocation);
                foreach (DatabaseLocation loc in session.DatabaseLocations)
                {
                    Console.WriteLine(loc.ToStringDetails(session, false));
                }
                place = new Placement(30001);
                for (int j = 1; j <= 5; j++)
                {
                    aMan = new Man(null, aMan, DateTime.UtcNow);
                    aMan.Persist(place, session);
                    aWoman = new Woman(aMan, aWoman);
                    aWoman.Persist(place, session);
                    aMan.m_spouse = new WeakIOptimizedPersistableReference <VelocityDbSchema.Person>(aWoman);
                    if (j % 1000 == 0)
                    {
                        session.FlushUpdates();
                    }
                }
                localLocation = new DatabaseLocation(systemHost3, systemDir, 40001, 50000, session, PageInfo.compressionKind.None, 0);
                session.NewLocation(localLocation);
                place = new Placement(40001);
                //localDatabase = session.NewDatabase(20001, localLocation);
                for (int j = 1; j <= 5; j++)
                {
                    aMan = new Man(null, aMan, DateTime.UtcNow);
                    aMan.Persist(place, session);
                    aWoman = new Woman(aMan, aWoman);
                    aWoman.Persist(place, session);
                    aMan.m_spouse = new WeakIOptimizedPersistableReference <VelocityDbSchema.Person>(aWoman);
                    if (j % 1000 == 0)
                    {
                        session.FlushUpdates();
                    }
                }
                session.Commit();
            }

            using (ServerClientSession session = new ServerClientSession(systemDir, systemHost))
            {
                session.CopyAllDatabasesTo(copyDir);
                using (SessionNoServer copySession = new SessionNoServer(copyDir))
                {
                    copySession.Verify();
                }
            }

            using (ServerClientSession session = new ServerClientSession(systemDir, systemHost, 2000, false)) // TO DO, change back to use optimistic locking
            {
                //session.SetTraceDbActivity(0);
                session.BeginUpdate();
                Database db = session.OpenDatabase(10000);
                session.DeleteDatabase(db);
                db = session.OpenDatabase(20001);
                session.DeleteDatabase(db);
                db = session.OpenDatabase(30001);
                session.DeleteDatabase(db);
                db = session.OpenDatabase(40001);
                session.DeleteDatabase(db);
                session.Commit();
                Directory.Delete(copyDir, true);
            }

            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();

            using (ServerClientSession session = new ServerClientSession(systemDir, systemHost))
            {
                Assert.True(session.OptimisticLocking);
            }
        }
 public void SingleServerReaderSingleServerUpdater2(bool useReaderCommit)
 {
   const UInt32 dbNum = 567;
   using (ServerClientSession updater = new ServerClientSession(systemDir))
   using (ServerClientSession reader = new ServerClientSession(systemDir, null, 2000, true, false, CacheEnum.No)) // CacheEnum.No or cache validating on session.Begin() - makes test fail
   {
     updater.BeginUpdate();
     Database db = updater.OpenDatabase(dbNum, true, false);
     if (db != null)
       updater.DeleteDatabase(db);
     updater.Commit();
     updater.BeginUpdate();
     Man man;
     Placement place = new Placement(dbNum, 1, 1, 2);
     for (int i = 0; i < 100; i++)
     {
       man = new Man();
       man.Persist(place, updater);
     }
     updater.Commit();
     reader.BeginRead();
     db = reader.OpenDatabase(dbNum);
     foreach (Page page in db)
       Assert.True(page.PageInfo.VersionNumber == 1);
     if (useReaderCommit)
       reader.Commit();
     updater.BeginUpdate();
     if (useReaderCommit)
       reader.BeginRead();
     else
       reader.ForceDatabaseCacheValidation();
     for (int i = 1; i < 25; i++)
     {
       db = reader.OpenDatabase(dbNum);
       foreach (Page page in db)
       {
         if (page.PageNumber > 0)
         {
           Assert.True(page.PageInfo.VersionNumber == (ulong)i);
           Man man2 = (Man)reader.Open(dbNum, page.PageNumber, 1, false);
           Man manUpdated = (Man)updater.Open(dbNum, page.PageNumber, 1, true);
         }
       }
       if (useReaderCommit)
       {
         reader.Commit();
         reader.BeginRead();
       }
       else
         reader.ForceDatabaseCacheValidation();
       updater.Commit();
       updater.BeginUpdate();
     }
     Database db2 = reader.OpenDatabase(dbNum);
     db = updater.OpenDatabase(dbNum);
     for (int i = 25; i < 50; i++)
     {
       foreach (Page page in db)
       {
         if (page.PageNumber > 0)
         {
           Assert.True(page.PageInfo.VersionNumber == (ulong)i);
           Man manUpdated = (Man)updater.Open(dbNum, page.PageNumber, 1, true);
         }
       }
       updater.FlushUpdates(); // now server will see updated version of pages
       foreach (Page page in db2)
       {
         if (page.PageNumber > 0)
         {                           // BUG Nov 8, 2011 1.0.4.0 reader sees version 28 when it should see version 27 
           Assert.True(page.PageInfo.VersionNumber == (ulong)i); // reader should see the commited version of the page, not the uncommited updated version
           Man man2 = (Man)reader.Open(dbNum, page.PageNumber, 1, false);
         }
       }
       reader.ClearPageCache(); // required or else we will use cached page and Assert (see line above) will fail
       System.GC.Collect(); // force weak referenced pages to be garbage collected (again to avoid Assert failure)
       updater.Commit();
       updater.BeginUpdate();
     }
     reader.Commit();
     updater.DeleteDatabase(db);
     updater.Commit();
   }
 }
 public void ServerPageFlush()
 {
     using (ServerClientSession session = new ServerClientSession(systemDir, systemHost))
     {
         //DatabaseLocation location = new DatabaseLocation(systemHost, location2Dir, 700, UInt32.MaxValue, session, false, PageInfo.encryptionKind.noEncryption);
         Placement place = new Placement(789, 1, 1, 1);
         //session.SetTraceAllDbActivity();
         session.BeginUpdate();
         session.RegisterClass(typeof(ObjWithArray));                                                  // avoid lock failure by registrty ahead of running parallell sessions.
         session.RegisterClass(typeof(Person));                                                        // avoid lock failure by registrty ahead of running parallell sessions.
         session.RegisterClass(typeof(VelocityDbSchema.Samples.AllSupportedSample.Pet));               // avoid lock failure by registrty ahead of running parallell sessions.
         session.RegisterClass(typeof(VelocityDbList <WeakIOptimizedPersistableReference <Person> >)); // avoid lock failure by registrty ahead of running parallell sessions.
         // session.NewLocation(location);
         session.Commit();
         session.BeginUpdate();
         for (int i = 0; i < 4000; i++)
         {
             ObjWithArray person = new ObjWithArray(i * 10);
             person.Persist(place, session);
         }
         session.FlushUpdates();
         for (int i = 0; i < 1000; i++)
         {
             ObjWithArray person = new ObjWithArray(i);
             person.Persist(place, session);
         }
         using (ServerClientSession session2 = new ServerClientSession(systemDir, systemHost))
         {
             Placement place2 = new Placement(7891, 1, 1, 1);
             session2.BeginUpdate();
             for (int i = 0; i < 1000; i++)
             {
                 ObjWithArray person = new ObjWithArray(i);
                 person.Persist(place2, session2);
             }
             session2.FlushUpdates();
             for (int i = 0; i < 1000; i++)
             {
                 ObjWithArray person = new ObjWithArray(i);
                 person.Persist(place2, session2);
             }
             session2.Commit();
         }
         using (ServerClientSession session2 = new ServerClientSession(systemDir, systemHost))
         {
             Placement place2 = new Placement(7894, 1, 1, 1);
             session2.BeginUpdate();
             for (int i = 0; i < 5000; i++)
             {
                 ObjWithArray person = new ObjWithArray(i);
                 person.Persist(place2, session2);
             }
             session2.FlushUpdates();
             for (int i = 0; i < 1000; i++)
             {
                 ObjWithArray person = new ObjWithArray(i);
                 person.Persist(place2, session2);
             }
             session2.Commit();
         }
         using (ServerClientSession session2 = new ServerClientSession(systemDir, systemHost))
         {
             Placement place2 = new Placement(7897, 1, 1, 1);
             session2.BeginUpdate();
             for (int i = 0; i < 5000; i++)
             {
                 ObjWithArray person = new ObjWithArray(i);
                 person.Persist(place2, session2);
             }
             session2.FlushUpdates();
             for (int i = 0; i < 1000; i++)
             {
                 ObjWithArray person = new ObjWithArray(i);
                 person.Persist(place2, session2);
             }
             using (ServerClientSession session3 = new ServerClientSession(systemDir, systemHost))
             {
                 Placement place3 = new Placement(7900, 1, 1, 1);
                 session3.BeginUpdate();
                 for (int i = 0; i < 1000; i++)
                 {
                     ObjWithArray person = new ObjWithArray(i);
                     person.Persist(place3, session3);
                 }
                 session3.FlushUpdates();
                 for (int i = 0; i < 1000; i++)
                 {
                     ObjWithArray person = new ObjWithArray(i);
                     person.Persist(place3, session3);
                 }
                 session.Commit();
                 session2.Commit();
                 session3.Commit();
             }
         }
     }
 }
 public void PagesWritten()
 {
     using (ServerClientSession session = new ServerClientSession(systemDir, systemHost))
     {
         Placement place = new Placement(789);
         session.BeginUpdate();
         session.RegisterClass(typeof(Person));
         session.RegisterClass(typeof(VelocityDbList <WeakIOptimizedPersistableReference <Person> >));
         session.RegisterClass(typeof(VelocityDbSchema.Samples.AllSupportedSample.Pet));
         session.RegisterClass(typeof(System.Guid));
         session.RegisterClass(typeof(AutoPlacement));
         session.Commit();
         session.BeginUpdate();
         Person person = new Person();
         person.m_friends.Persist(place, session);
         person.Persist(place, session);
         session.FlushUpdates();
         person = new Person();
         person.Persist(place, session);
         using (ServerClientSession session2 = new ServerClientSession(systemDir, systemHost))
         {
             Placement place2 = new Placement(7891);
             session2.BeginUpdate();
             Person person2 = new Person();
             person2.m_friends.Persist(place2, session2);
             person2.Persist(place2, session2);
             session2.FlushUpdates();
             person2 = new Person();
             person2.Persist(place2, session2);
             session2.Commit();
         }
         using (ServerClientSession session2 = new ServerClientSession(systemDir, systemHost))
         {
             Placement place2 = new Placement(7892);
             session2.BeginUpdate();
             Person person2 = new Person();
             person2.m_friends.Persist(place2, session2);
             person2.Persist(place2, session2);
             session2.FlushUpdates();
             person2 = new Person();
             person2.m_friends.Persist(place2, session2);
             person2.Persist(place2, session2);
             session2.Commit();
         }
         using (ServerClientSession session2 = new ServerClientSession(systemDir, systemHost))
         {
             Placement place2 = new Placement(7893);
             session2.BeginUpdate();
             Person person2 = new Person();
             person2.m_friends.Persist(place2, session2);
             person2.Persist(place2, session2);
             session2.FlushUpdates();
             person2 = new Person();
             person2.Persist(place2, session2);
             using (ServerClientSession session3 = new ServerClientSession(systemDir, systemHost))
             {
                 Placement place3 = new Placement(7894);
                 session3.BeginUpdate();
                 Person person3 = new Person();
                 person3.m_friends.Persist(place3, session3);
                 person3.Persist(place3, session3);
                 session3.FlushUpdates();
                 person3 = new Person();
                 person3.m_friends.Persist(place3, session3);
                 person3.Persist(place3, session3);
                 session.Commit();
                 session2.Commit();
                 session3.Commit();
             }
         }
     }
 }
        public void SingleServerReaderSingleServerUpdater2(bool useReaderCommit)
        {
            const UInt32 dbNum = 567;

            using (ServerClientSession updater = new ServerClientSession(systemDir))
                using (ServerClientSession reader = new ServerClientSession(systemDir, null, 2000, true, false, CacheEnum.No)) // CacheEnum.No or cache validating on session.Begin() - makes test fail
                {
                    updater.BeginUpdate();
                    Database db = updater.OpenDatabase(dbNum, true, false);
                    if (db != null)
                    {
                        updater.DeleteDatabase(db);
                    }
                    updater.Commit();
                    updater.BeginUpdate();
                    Man       man;
                    Placement place = new Placement(dbNum, 1, 1, 2);
                    for (int i = 0; i < 100; i++)
                    {
                        man = new Man();
                        man.Persist(place, updater);
                    }
                    updater.Commit();
                    reader.BeginRead();
                    db = reader.OpenDatabase(dbNum);
                    foreach (Page page in db)
                    {
                        Assert.True(page.PageInfo.VersionNumber == 1);
                    }
                    if (useReaderCommit)
                    {
                        reader.Commit();
                    }
                    updater.BeginUpdate();
                    if (useReaderCommit)
                    {
                        reader.BeginRead();
                    }
                    else
                    {
                        reader.ForceDatabaseCacheValidation();
                    }
                    for (int i = 1; i < 25; i++)
                    {
                        db = reader.OpenDatabase(dbNum);
                        foreach (Page page in db)
                        {
                            if (page.PageNumber > 0)
                            {
                                Assert.True(page.PageInfo.VersionNumber == (ulong)i);
                                Man man2       = (Man)reader.Open(dbNum, page.PageNumber, 1, false);
                                Man manUpdated = (Man)updater.Open(dbNum, page.PageNumber, 1, true);
                            }
                        }
                        if (useReaderCommit)
                        {
                            reader.Commit();
                            reader.BeginRead();
                        }
                        else
                        {
                            reader.ForceDatabaseCacheValidation();
                        }
                        updater.Commit();
                        updater.BeginUpdate();
                    }
                    Database db2 = reader.OpenDatabase(dbNum);
                    db = updater.OpenDatabase(dbNum);
                    for (int i = 25; i < 50; i++)
                    {
                        foreach (Page page in db)
                        {
                            if (page.PageNumber > 0)
                            {
                                Assert.True(page.PageInfo.VersionNumber == (ulong)i);
                                Man manUpdated = (Man)updater.Open(dbNum, page.PageNumber, 1, true);
                            }
                        }
                        updater.FlushUpdates(); // now server will see updated version of pages
                        foreach (Page page in db2)
                        {
                            if (page.PageNumber > 0)
                            {                                                         // BUG Nov 8, 2011 1.0.4.0 reader sees version 28 when it should see version 27
                                Assert.True(page.PageInfo.VersionNumber == (ulong)i); // reader should see the commited version of the page, not the uncommited updated version
                                Man man2 = (Man)reader.Open(dbNum, page.PageNumber, 1, false);
                            }
                        }
                        reader.ClearPageCache(); // required or else we will use cached page and Assert (see line above) will fail
                        System.GC.Collect();     // force weak referenced pages to be garbage collected (again to avoid Assert failure)
                        updater.Commit();
                        updater.BeginUpdate();
                    }
                    reader.Commit();
                    updater.DeleteDatabase(db);
                    updater.Commit();
                }
        }
    public void multipleServersInvalid()
    {
      Assert.Throws<InvalidChangeOfDefaultLocationException>(() =>
      {
        using (ServerClientSession session = new ServerClientSession(systemDir))
        {
          session.SetTraceDbActivity(2);
          try
          {
            DatabaseLocation localLocation = new DatabaseLocation(systemHost, location2Dir, 10000, 20000, session, PageInfo.compressionKind.LZ4, 0);
            Placement place = new Placement(10000, 2);
            session.BeginUpdate();
            session.NewLocation(localLocation);
            Man aMan = null;
            Woman aWoman = null;

            for (int j = 1; j <= 5; j++)
            {
              aMan = new Man(null, aMan, DateTime.UtcNow);
              aMan.Persist(place, session);
              aWoman = new Woman(aMan, aWoman);
              aWoman.Persist(place, session);
              aMan.m_spouse = new WeakIOptimizedPersistableReference<VelocityDbSchema.Person>(aWoman);
              if (j % 1000 == 0)
              {
                session.FlushUpdates();
              }
            }
            localLocation = new DatabaseLocation(systemHost, systemDir, 20001, 30000, session, PageInfo.compressionKind.None, 0);
            session.NewLocation(localLocation);
            place = new Placement(20001);
            //localDatabase = session.NewDatabase(20001, localLocation);
            for (int j = 1; j <= 5; j++)
            {
              aMan = new Man(null, aMan, DateTime.UtcNow);
              aMan.Persist(place, session);
              aWoman = new Woman(aMan, aWoman);
              aWoman.Persist(place, session);
              aMan.m_spouse = new WeakIOptimizedPersistableReference<VelocityDbSchema.Person>(aWoman);
              if (j % 1000 == 0)
              {
                session.FlushUpdates();
              }
            }
            DatabaseLocation serverLocation = new DatabaseLocation(systemHost2, location2Dir, 30001, 40000, session, PageInfo.compressionKind.LZ4, 0);
            session.NewLocation(serverLocation);
            place = new Placement(30001);
            for (int j = 1; j <= 5; j++)
            {
              aMan = new Man(null, aMan, DateTime.UtcNow);
              aMan.Persist(place, session);
              aWoman = new Woman(aMan, aWoman);
              aWoman.Persist(place, session);
              aMan.m_spouse = new WeakIOptimizedPersistableReference<VelocityDbSchema.Person>(aWoman);
              if (j % 1000 == 0)
              {
                session.FlushUpdates();
              }
            }
            session.Commit();
          }
          finally
          {
            //session.Close();
          }
        }
      });
    }
    [Repeat(3)] // remove when propagation of optimistic locking flag is done to slave database locations TO DO (issue caused by CopyAllDatabasdesTo that uses pessimistic locking)
    public void multipleServersOK()
    {
      using (SessionNoServer session = new SessionNoServer(systemDir))
      {
        session.BeginRead();
        foreach (DatabaseLocation loc in session.DatabaseLocations)
          Console.WriteLine(loc.ToStringDetails(session, false));
        session.Commit();
      }

      using (ServerClientSession session = new ServerClientSession(systemDir, systemHost))
      {
        session.SetTraceDbActivity(0);
        DatabaseLocation localLocation = new DatabaseLocation(systemHost, location2Dir, 10000, 20000, session, PageInfo.compressionKind.LZ4, 0);
        Placement place = new Placement(10000, 2);
        session.BeginUpdate();
        foreach (DatabaseLocation loc in session.DatabaseLocations)
          Console.WriteLine(loc.ToStringDetails(session, false));
        Console.WriteLine();
        session.NewLocation(localLocation);
        foreach (DatabaseLocation loc in session.DatabaseLocations)
          Console.WriteLine(loc.ToStringDetails(session, false));
        Man aMan = null;
        Woman aWoman = null;

        for (int j = 1; j <= 5; j++)
        {
          aMan = new Man(null, aMan, DateTime.UtcNow);
          aMan.Persist(place, session);
          aWoman = new Woman(aMan, aWoman);
          aWoman.Persist(place, session);
          aMan.m_spouse = new WeakIOptimizedPersistableReference<VelocityDbSchema.Person>(aWoman);
          if (j % 1000 == 0)
          {
            session.FlushUpdates();
          }
        }
        localLocation = new DatabaseLocation(systemHost2, systemDir, 20001, 30000, session, PageInfo.compressionKind.LZ4, 0);
        session.NewLocation(localLocation);
        foreach (DatabaseLocation loc in session.DatabaseLocations)
          Console.WriteLine(loc.ToStringDetails(session, false));
        place = new Placement(20001);
        //localDatabase = session.NewDatabase(20001, localLocation);
        for (int j = 1; j <= 5; j++)
        {
          aMan = new Man(null, aMan, DateTime.UtcNow);
          aMan.Persist(place, session);
          aWoman = new Woman(aMan, aWoman);
          aWoman.Persist(place, session);
          aMan.m_spouse = new WeakIOptimizedPersistableReference<VelocityDbSchema.Person>(aWoman);
          if (j % 1000 == 0)
          {
            session.FlushUpdates();
          }
        }
        DatabaseLocation serverLocation = new DatabaseLocation(systemHost2, location2Dir, 30001, 40000, session, PageInfo.compressionKind.LZ4, 0);
        session.NewLocation(serverLocation);
        foreach (DatabaseLocation loc in session.DatabaseLocations)
          Console.WriteLine(loc.ToStringDetails(session, false));
        place = new Placement(30001);
        for (int j = 1; j <= 5; j++)
        {
          aMan = new Man(null, aMan, DateTime.UtcNow);
          aMan.Persist(place, session);
          aWoman = new Woman(aMan, aWoman);
          aWoman.Persist(place, session);
          aMan.m_spouse = new WeakIOptimizedPersistableReference<VelocityDbSchema.Person>(aWoman);
          if (j % 1000 == 0)
          {
            session.FlushUpdates();
          }
        }
        localLocation = new DatabaseLocation(systemHost3, systemDir, 40001, 50000, session, PageInfo.compressionKind.None, 0);
        session.NewLocation(localLocation);
        place = new Placement(40001);
        //localDatabase = session.NewDatabase(20001, localLocation);
        for (int j = 1; j <= 5; j++)
        {
          aMan = new Man(null, aMan, DateTime.UtcNow);
          aMan.Persist(place, session);
          aWoman = new Woman(aMan, aWoman);
          aWoman.Persist(place, session);
          aMan.m_spouse = new WeakIOptimizedPersistableReference<VelocityDbSchema.Person>(aWoman);
          if (j % 1000 == 0)
          {
            session.FlushUpdates();
          }
        }
        session.Commit();
      }

      using (ServerClientSession session = new ServerClientSession(systemDir, systemHost))
      {
        session.CopyAllDatabasesTo(copyDir);
        using (SessionNoServer copySession = new SessionNoServer(copyDir))
        {
          copySession.Verify();
        }
      }

      using (ServerClientSession session = new ServerClientSession(systemDir, systemHost, 2000, false)) // TO DO, change back to use optimistic locking
      {
        //session.SetTraceDbActivity(0);
        session.BeginUpdate();
        Database db = session.OpenDatabase(10000);
        session.DeleteDatabase(db);
        db = session.OpenDatabase(20001);
        session.DeleteDatabase(db);
        db = session.OpenDatabase(30001);
        session.DeleteDatabase(db);
        db = session.OpenDatabase(40001);
        session.DeleteDatabase(db);
        session.Commit();
        Directory.Delete(copyDir, true);
      }

      System.GC.Collect();
      System.GC.WaitForPendingFinalizers();

      using (ServerClientSession session = new ServerClientSession(systemDir, systemHost))
      {
        Assert.True(session.OptimisticLocking);
      }
    }