void LockConflict(SessionBase sharedReadSession) { string host = null; Random r = new Random(5); SessionPool sessionPool = new SessionPool(3, () => new ServerClientSession(systemDir, host, 2000, false)); try { int iCounter = 0; int sessionId1 = -1; SessionBase session1 = null; for (int i = 0; i < 50; i++) { try { session1 = sessionPool.GetSession(out sessionId1); session1.BeginUpdate(); Dokument Doc_A = new Dokument(); Doc_A.Name = "Test A"; session1.Persist(Doc_A); Console.WriteLine(Doc_A.ToString()); int sessionId2 = -1; SessionBase session2 = null; try { session2 = sessionPool.GetSession(out sessionId2); session2.BeginUpdate(); Dokument Doc_B = new Dokument(); Doc_B.Name = "Test_B"; session2.Persist(Doc_B); Console.WriteLine(Doc_B.ToString()); session2.Commit(); } finally { sessionPool.FreeSession(sessionId2, session2); } session1.Commit(); sharedReadSession.ForceDatabaseCacheValidation(); session1.BeginRead(); ulong ct = session1.AllObjects <Dokument>(false).Count; Console.WriteLine("Number of Dokument found by normal session: " + ct); session1.Commit(); ct = sharedReadSession.AllObjects <Dokument>(false).Count; Console.WriteLine("Number of Dokument found by shared read session: " + ct); } finally { sessionPool.FreeSession(sessionId1, session1); } iCounter++; Console.WriteLine(" -> " + iCounter.ToString()); } } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } }
public void sessionPoolTest() { const int numberOfSessions = 5; using (SessionPool pool = new SessionPool(numberOfSessions, () => new SessionNoServer(systemDir))) { { int sessionId = -1; SessionBase session = null; try { session = pool.GetSession(out sessionId); using (SessionBase.Transaction transaction = session.BeginUpdate()) { for (int i = 0; i < 1000; i++) { Man man = new Man(); session.Persist(man); } session.Commit(); } } catch (Exception e) { Console.WriteLine(e.Message); throw e; } finally { pool.FreeSession(sessionId, session); } } Parallel.ForEach(Enumerable.Range(0, numberOfSessions * 5), x => { int sessionId = -1; SessionBase session = null; try { session = pool.GetSession(out sessionId); if (session.InTransaction == false) { session.BeginRead(); } var allMen = session.AllObjects <Man>(); ulong allMenCt = allMen.Count(); foreach (Man man in allMen) { double lat = man.Latitude; } Console.WriteLine("Man Count is: " + allMenCt + " Session Id is: " + sessionId + " Current task id is: " + (Task.CurrentId.HasValue ? Task.CurrentId.Value.ToString() : "unknown")); } catch (Exception e) { session?.Abort(); Console.WriteLine(e.Message); throw e; } finally { pool.FreeSession(sessionId, session); } }); } }
void LockConflict(SessionBase sharedReadSession) { string host = null; Random r = new Random(5); SessionPool sessionPool = new SessionPool(3, () => new ServerClientSession(systemDir, host, 2000, false)); try { int iCounter = 0; int sessionId1 = -1; SessionBase session1 = null; for (int i = 0; i < 50; i++) { try { session1 = sessionPool.GetSession(out sessionId1); session1.BeginUpdate(); Dokument Doc_A = new Dokument(); Doc_A.Name = "Test A"; session1.Persist(Doc_A); Console.WriteLine(Doc_A.ToString()); int sessionId2 = -1; SessionBase session2 = null; try { session2 = sessionPool.GetSession(out sessionId2); session2.BeginUpdate(); Dokument Doc_B = new Dokument(); Doc_B.Name = "Test_B"; session2.Persist(Doc_B); Console.WriteLine(Doc_B.ToString()); session2.Commit(); } finally { sessionPool.FreeSession(sessionId2, session2); } session1.Commit(); sharedReadSession.ForceDatabaseCacheValidation(); session1.BeginRead(); ulong ct = session1.AllObjects<Dokument>(false).Count; Console.WriteLine("Number of Dokument found by normal session: " + ct); session1.Commit(); ct = sharedReadSession.AllObjects<Dokument>(false).Count; Console.WriteLine("Number of Dokument found by shared read session: " + ct); } finally { sessionPool.FreeSession(sessionId1, session1); } iCounter++; Console.WriteLine(" -> " + iCounter.ToString()); } } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } }