Show Blogger Panel Hide Blogger Panel
Alex Yakunin

February 23, 2010

Versions and locking tests (code samples for Manual): the links

See:
VersionsTest output:
Entity: Person('Alex Yakunin')
          Key: Person, (1)
  VersionInfo: (Alex, Yakunin, null)


Entity: Person('Dmitri Maximov')
          Key: Person, (2)
  VersionInfo: (Dmitri, Maximov, null)


Entity: Company('X-tensive.com', Employees: )
          Key: Company, (3)
  VersionInfo: (0)


Changing X-tensive.com name to Xtensive
Entity: Company('Xtensive', Employees: )
          Key: Company, (3)
  VersionInfo: (1)


Xtensive.Employees.Add(Alex)
Entity: Company('Xtensive', Employees: Person('Alex Yakunin'))
          Key: Company, (3)
  VersionInfo: (2)


Dmitri.Company = Xtensive
Entity: Company('Xtensive', Employees: Person('Dmitri Maximov'), Person('Alex Yakunin'))
          Key: Company, (3)
  VersionInfo: (3)


Transaction rollback test, before:
Entity: Company('Xtensive', Employees: Person('Dmitri Maximov'), Person('Alex Yakunin'))
          Key: Company, (3)
  VersionInfo: (3)


Transaction rollback test, inside:
Entity: Company('Xtensive', Employees: )
          Key: Company, (3)
  VersionInfo: (4)


Transaction rollback test, after:
Entity: Company('Xtensive', Employees: Person('Dmitri Maximov'), Person('Alex Yakunin'))
          Key: Company, (3)
  VersionInfo: (3)

LockingTest output:
This one is really large, but it shows what's happening pretty well.
LockingTest, LockingMode=None, IsolationLevel = ReadCommitted
Counter isn't isolated.
  Thread 1: beginning of transaction
       Thread 2: beginning of transaction
  Thread 1:   reading shared counter
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
  Thread 1:   delay (500ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 1
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 1
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading shared counter
  Thread 1:   delay (500ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 2
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 2
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading shared counter
  Thread 1:   delay (500ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 3
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 4
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 3
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading shared counter
  Thread 1:   delay (500ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 5
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 4
  Thread 1:   committing transaction
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 6
       Thread 2:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading shared counter
  Thread 1:   delay (500ms)
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 7
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 7
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading shared counter
  Thread 1:   delay (500ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 8
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 8
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading shared counter
  Thread 1:   delay (500ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 9
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 10
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 9
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading shared counter
  Thread 1:   delay (500ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 11
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 10
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading shared counter
  Thread 1:   delay (500ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 12
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 13
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 11
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading shared counter
  Thread 1:   delay (500ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 14
       Thread 2:   committing transaction
       Thread 2: transaction is committed
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 12
  Thread 1:   committing transaction
  Thread 1: transaction is committed
Final counter.Value = 12


LockingTest, LockingMode=None, IsolationLevel = RepeatableRead
There are deadlocks or version conflicts, but counter is isolated.
  Thread 1: beginning of transaction
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
  Thread 1:   reading shared counter
  Thread 1:   delay (500ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 1
       Thread 2:   committing transaction
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 1
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading shared counter
  Thread 1:   delay (500ms)
       Thread 2:   error: DeadlockException
       Thread 2: transaction is rolled back
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
       Thread 2:   delay (333ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 2
       Thread 2:   committing transaction
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 2
  Thread 1:   committing transaction
       Thread 2:   error: DeadlockException
       Thread 2: transaction is rolled back
  Thread 1: transaction is committed
Final counter.Value = 2


LockingTest, LockingMode=EntityLock, IsolationLevel = RepeatableRead
Still can be a deadlock, since lock happen after read.
  Thread 1: beginning of transaction
       Thread 2: beginning of transaction
  Thread 1:   reading shared counter
  Thread 1:   locking counter
       Thread 2:   reading shared counter
       Thread 2:   locking counter
       Thread 2:   counter is locked
       Thread 2:   delay (333ms)
  Thread 1:   error: DeadlockException
  Thread 1: transaction is rolled back
  Thread 1: beginning of transaction
  Thread 1:   reading shared counter
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 1
       Thread 2:   committing transaction
       Thread 2: transaction is committed
  Thread 1:   locking counter
  Thread 1:   counter is locked
  Thread 1:   delay (500ms)
       Thread 2: beginning of transaction
       Thread 2:   reading shared counter
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 2
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading shared counter
       Thread 2:   locking counter
  Thread 1:   locking counter
  Thread 1:   counter is locked
  Thread 1:   delay (500ms)
       Thread 2:   error: DeadlockException
       Thread 2: transaction is rolled back
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 3
  Thread 1:   committing transaction
  Thread 1: transaction is committed
Final counter.Value = 3


LockingTest, LockingMode=QueryLock, IsolationLevel = RepeatableRead
No deadlocks, counter isolation.
  Thread 1: beginning of transaction
       Thread 2: beginning of transaction
  Thread 1:   reading & locking shared counter
       Thread 2:   reading & locking shared counter
  Thread 1:   delay (500ms)
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 1
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading & locking shared counter
       Thread 2:   delay (333ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 2
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading & locking shared counter
  Thread 1:   delay (500ms)
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 3
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading & locking shared counter
       Thread 2:   delay (333ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 4
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading & locking shared counter
  Thread 1:   delay (500ms)
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 5
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading & locking shared counter
       Thread 2:   delay (333ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 6
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
  Thread 1:   delay (500ms)
       Thread 2:   reading & locking shared counter
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 7
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading & locking shared counter
       Thread 2:   delay (333ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 8
       Thread 2:   committing transaction
       Thread 2: transaction is committed
       Thread 2: beginning of transaction
       Thread 2:   reading & locking shared counter
  Thread 1:   delay (500ms)
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 9
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading & locking shared counter
       Thread 2:   delay (333ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 10
       Thread 2:   committing transaction
       Thread 2: transaction is committed
  Thread 1:   delay (500ms)
       Thread 2: beginning of transaction
       Thread 2:   reading & locking shared counter
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 11
  Thread 1:   committing transaction
  Thread 1: transaction is committed
  Thread 1: beginning of transaction
  Thread 1:   reading & locking shared counter
       Thread 2:   delay (333ms)
       Thread 2:   incrementing counter
       Thread 2:   counter.Value = 12
       Thread 2:   committing transaction
       Thread 2: transaction is committed
  Thread 1:   delay (500ms)
  Thread 1:   incrementing counter
  Thread 1:   counter.Value = 13
  Thread 1:   committing transaction
  Thread 1: transaction is committed
Final counter.Value = 13