Skip to content

Instantly share code, notes, and snippets.

@chilversc
Created March 6, 2013 15:52
Show Gist options
  • Save chilversc/5100296 to your computer and use it in GitHub Desktop.
Save chilversc/5100296 to your computer and use it in GitHub Desktop.
NHibernate Contains query bug - mutating a collection after a query has executed breaks queries in subsequent sessions
[Test]
public void UsersWithListContains_MutatingListDoesNotBreakOtherSessions()
{
using (var firstSession = OpenSession ()) {
var names = new List<string> { "ayende", "rahien" };
var query = (from user in firstSession.Query<User> ()
where names.Contains(user.Name)
select user).ToList();
names.Clear ();
Assert.AreEqual(2, query.Count);
}
using (var secondSession = OpenSession ()) {
var names = new List<string> { "ayende" };
var query = (from user in secondSession.Query<User> ()
where names.Contains(user.Name)
select user).ToList();
// This line fails with Expected: 1 But was: 0
// The SQL in NHProf shows that the where clause was executed as WHERE 1 = 0 as if names were empty
Assert.AreEqual(1, query.Count);
}
}
@chilversc
Copy link
Author

The bug is caused by line 11, names.Clear()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment