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 ;
@@ -10,7 +11,6 @@ namespace NHibernate.Test.Linq
10
11
{
11
12
public class QueryLock : LinqTestCase
12
13
{
13
-
14
14
[ Test ]
15
15
public void CanSetLockLinqQueries ( )
16
16
{
@@ -40,7 +40,6 @@ public void CanSetLockOnLinqPagingQuery()
40
40
}
41
41
}
42
42
43
-
44
43
[ Test ]
45
44
public void CanLockBeforeSkipOnLinqOrderedPageQuery ( )
46
45
{
@@ -49,15 +48,14 @@ public void CanLockBeforeSkipOnLinqOrderedPageQuery()
49
48
var result = ( from e in db . Customers
50
49
orderby e . CompanyName
51
50
select e )
52
- . SetLockMode ( LockMode . Upgrade ) . Skip ( 5 ) . Take ( 5 ) . ToList ( ) ;
51
+ . SetLockMode ( LockMode . Upgrade ) . Skip ( 5 ) . Take ( 5 ) . ToList ( ) ;
53
52
54
53
Assert . That ( result , Has . Count . EqualTo ( 5 ) ) ;
55
54
Assert . That ( session . GetCurrentLockMode ( result [ 0 ] ) , Is . EqualTo ( LockMode . Upgrade ) ) ;
56
55
AssertSeparateTransactionIsLockedOut ( result [ 0 ] . CustomerId ) ;
57
56
}
58
57
}
59
58
60
-
61
59
private void AssertSeparateTransactionIsLockedOut ( string customerId )
62
60
{
63
61
using ( new TransactionScope ( TransactionScopeOption . Suppress ) )
@@ -68,14 +66,49 @@ private void AssertSeparateTransactionIsLockedOut(string customerId)
68
66
Assert . Throws < GenericADOException > (
69
67
( ) =>
70
68
{
71
- var result2 = ( from e in s2 . Query < Customer > ( )
72
- where e . CustomerId == customerId
73
- select e ) . SetLockMode ( LockMode . UpgradeNoWait )
74
- . Timeout ( 5 ) . ToList ( ) ;
75
- Assert . IsNotNull ( result2 ) ;
76
- } , "Expected an exception to indicate locking failure due to already locked." ) ;
69
+ var result2 = (
70
+ from e in s2 . Query < Customer > ( )
71
+ where e . CustomerId == customerId
72
+ select e
73
+ ) . SetLockMode ( LockMode . UpgradeNoWait )
74
+ . WithOptions ( o => o . SetTimeout ( 5 ) )
75
+ . ToList ( ) ;
76
+ Assert . That ( result2 , Is . Not . Null ) ;
77
+ } ,
78
+ "Expected an exception to indicate locking failure due to already locked." ) ;
77
79
}
78
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 )
98
+ . ToList ( ) ;
99
+ Assert . That ( result2 , Has . Count . EqualTo ( 91 ) ) ;
100
+
101
+ Assert . That ( logSpy . GetWholeLog ( ) . ToLower ( ) , Does . Contain ( "nowait" ) ) ;
102
+ }
103
+ }
104
+ }
105
+
106
+ private static IQueryable < Customer > BuildQueryableAllCustomers (
107
+ IQueryable < Customer > dbCustomers ,
108
+ LockMode lockMode )
109
+ {
110
+ return ( from e in dbCustomers select e ) . SetLockMode ( lockMode ) . WithOptions ( o => o . SetTimeout ( 5 ) ) ;
111
+ }
79
112
}
80
113
}
81
114
0 commit comments