I am wondering under what circumstances the following NHibernate code may crash:
var session = NHibernateSessionManager.CurrentSession;
var foo = session.Linq<Foo>.ToList()[0];
foo.SomeProperty = "test";
session.SaveOrUpdate(foo);
var reloadedFoos = session.Linq<Foo>
.Where(x => x.SomeProperty == "test");
Assert.That(reloadedFoos.Count > 0);
The Assert statement always fails.
If I manually call session.Flush after SaveOrUpdate, the selection request will succeed, however, I thought we did not need to manually call flush? I realized that NHibernate must be smart enough to understand that Foo has been updated, so the second select request must be successful.
Watching the generated SQL, a second SQL query appears, which is executed before the first SaveOrUpdate SQL code.
In fact, if I complete the entire method in a transaction, it will succeed:
using(NHibernateSessionManager.CurrentSession.BeginTransaction()
{
}
Now sql SaveOrUpdate will be executed before linq.Where sql. This is a little strange, since I donโt even need to make a transaction between them.
What's happening?