Reloading data changes done from another session

12 views
Skip to first unread message

Karl Cassar

unread,
Feb 8, 2012, 7:31:32 AM2/8/12
to nhu...@googlegroups.com
I've been having a lot of trouble in refreshing data from the database.  I've created a 'ConcurrencyTest' class, in order to try and simulate this functionality, but could not get it to work using the methods which I think I am supposed to use based on the NH Manual.

private long _testID = 0;
        [Test]
        public void test1()
        {

            using (var t = beginTransaction())
            {
                Brand b = Brand.Factory.CreateNewItem();
                b.Title = "Hello";
                b.Save();
                _testID = b.ID;
                t.Commit();
            } //saves a new brand, with title as 'Hello'
            disposeCurrentSession();
            CallMethodOnSeperateThread(thread1);
            CallMethodOnSeperateThread(thread2);

            System.Threading.Thread.Sleep(15000);


        }

        private void thread1()
        {
            System.Threading.Thread.Sleep(1000); // wait so that thread thread 2 has chance to load it
                
            createNewSession();

            using (var t = beginTransaction())
            {
                Brand b = Brand.Factory.GetByPrimaryKey(_testID);
                b.Title = "Updated by thread 1";
                b.Save();
                t.Commit();
            }
            disposeCurrentSession();
        }
        private void thread2()
        {
            ISession session = (ISession) createNewSession();
            session.CacheMode = CacheMode.Ignore;
            
            using (var t = beginTransaction())
            {


                Brand b = session.Get<Brand>(_testID);
                
                System.Threading.Thread.Sleep(3000);

                Assert.That(b.Title, Is.Not.EqualTo("Updated by thread 1"));
                session.Lock(b, LockMode.Read);
                
                session.Refresh(b);
                Assert.That(b.Title, Is.EqualTo("Updated by thread 1")); // This should be true, but never is!



                b.Title = "Updated by thread 2";
                
                b.Save();
                t.Commit();
            }

            disposeCurrentSession();
        }

---
There are several utility methods used, but you should be able to get an idea what they should do.

A summary of what the above test does is this:  A new brand is created, and the ID stored.  Then, two threads are spawned each using a distinct NH session, and both load the same brand. Thread #2 waits 5 seconds, so that it gives time for thread #1 to update it.  Thread #1 updates the title to 'Updated by thread 1'.  When Thread #2 returns from sleep, it firsts checks that the title is still 'Hello', then it Refreshes it, and checks that it is 'Updated by thread 1'.  However, this never passes!

Any ideas why would be greatly appreciated!
Reply all
Reply to author
Forward
0 new messages