|
1 | 1 | using System.Linq;
|
2 | 2 | using System.Transactions;
|
| 3 | +using NHibernate.Dialect; |
3 | 4 | using NHibernate.DomainModel.Northwind.Entities;
|
4 | 5 | using NHibernate.Exceptions;
|
5 | 6 | using NHibernate.Linq;
|
@@ -76,6 +77,36 @@ private void AssertSeparateTransactionIsLockedOut(string customerId)
|
76 | 77 | }, "Expected an exception to indicate locking failure due to already locked.");
|
77 | 78 | }
|
78 | 79 | }
|
| 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 | + } |
79 | 110 | }
|
80 | 111 | }
|
81 | 112 |
|
|
0 commit comments