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!
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/nhusers/-/s4QNudofmdwJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.