多くのグーグル検索の後、私はそれを行うためのかなり簡単な方法を見つけました。これを再現する手順は次のとおりです。
Get() メソッドと Update() メソッドの間に Thread.Sleep() を、1 人のユーザー (プロセス 1 ) に対してのみ配置します。
プロセス 1 が実行されているときに、Thread.Sleep() に遭遇しないプロセス 2 を開始し、プロセス 1 が実行する前に更新を完了します。
プロセス 2 がデータベース内のデータを変更した後、プロセス 1 がデータを更新しようとすると、NHibernate は古いオブジェクト例外をスローします。
次のコード スニペットを参照してください。
public void Update(int empid)
{
Employee person = this.dalService.GetByEntityId(empid);
person.Name = "Process 1";
// At this point , Update the Person table manually using raw sql query
// such as this 'Update Person Set Name = 'Process 2'where empid = @empid;
// When the update is performed , it is expected to throw the exception , as the in memory data
// is different from the one in database.
if (username.equals("Bob"))
{
Thread.Sleep(50000);
// If this is a website, have the above condition, so that it is simulated only for one user.
}
this.dalService.Update(person);
}