Skip to content

Commit 7404dc7

Browse files
committed
QueryLock: Add test case to verify that a change in lock mode will avoid reusing cached query.
1 parent 5fe2e92 commit 7404dc7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/NHibernate.Test/Linq/QueryLock.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Linq;
22
using System.Transactions;
3+
using NHibernate.Dialect;
34
using NHibernate.DomainModel.Northwind.Entities;
45
using NHibernate.Exceptions;
56
using NHibernate.Linq;
@@ -76,6 +77,36 @@ private void AssertSeparateTransactionIsLockedOut(string customerId)
7677
}, "Expected an exception to indicate locking failure due to already locked.");
7778
}
7879
}
80+
81+
82+
[Test]
83+
[Description("Verify that different lock modes are respected even if the query is otherwise exactly the same.")]
84+
public void CanChangeLockModeForQuery()
85+
{
86+
// Limit to a few dialects where we know the "nowait" keyword is used to make life easier.
87+
Assume.That(Dialect is MsSql2000Dialect || Dialect is Oracle8iDialect || Dialect is PostgreSQL81Dialect);
88+
89+
using (session.BeginTransaction())
90+
{
91+
var result = BuildQueryableAllCustomers(db.Customers, LockMode.Upgrade).ToList();
92+
Assert.That(result, Has.Count.EqualTo(91));
93+
94+
using (var logSpy = new SqlLogSpy())
95+
{
96+
// Only difference in query is the lockmode - make sure it gets picked up.
97+
var result2 = BuildQueryableAllCustomers(session.Query<Customer>(), LockMode.UpgradeNoWait).ToList();
98+
Assert.That(result2, Has.Count.EqualTo(91));
99+
100+
Assert.That(logSpy.GetWholeLog().ToLower(), Is.StringContaining("nowait"));
101+
}
102+
}
103+
}
104+
105+
private static IQueryable<Customer> BuildQueryableAllCustomers(IQueryable<Customer> dbCustomers, LockMode lockMode)
106+
{
107+
return (from e in dbCustomers
108+
select e).SetLockMode(lockMode).Timeout(5);
109+
}
79110
}
80111
}
81112

0 commit comments

Comments
 (0)