1
1
using System . Linq ;
2
- using NHibernate . AdoNet ;
3
- using NHibernate . Cfg ;
4
- using NHibernate . Engine ;
2
+ using System . Transactions ;
3
+ using NHibernate . DomainModel . Northwind . Entities ;
4
+ using NHibernate . Exceptions ;
5
5
using NHibernate . Linq ;
6
6
using NUnit . Framework ;
7
7
8
+
8
9
namespace NHibernate . Test . Linq
9
10
{
10
11
public class QueryLock : LinqTestCase
@@ -13,35 +14,69 @@ public class QueryLock : LinqTestCase
13
14
[ Test ]
14
15
public void CanSetLockLinqQueries ( )
15
16
{
16
- var result = ( from e in db . Customers
17
- where e . CompanyName == "Corp"
18
- select e ) . SetLockMode ( LockMode . Upgrade ) . ToList ( ) ;
17
+ using ( session . BeginTransaction ( ) )
18
+ {
19
+ var result = ( from e in db . Customers
20
+ select e ) . SetLockMode ( LockMode . Upgrade ) . ToList ( ) ;
19
21
22
+ Assert . That ( result , Has . Count . EqualTo ( 91 ) ) ;
23
+ Assert . That ( session . GetCurrentLockMode ( result [ 0 ] ) , Is . EqualTo ( LockMode . Upgrade ) ) ;
24
+ AssertSeparateTransactionIsLockedOut ( result [ 0 ] . CustomerId ) ;
25
+ }
20
26
}
21
27
22
28
23
29
[ Test ]
24
30
public void CanSetLockOnLinqPagingQuery ( )
25
31
{
26
- var result = ( from e in db . Customers
27
- where e . CompanyName == "Corp"
28
- select e ) . Skip ( 5 ) . Take ( 5 ) . SetLockMode ( LockMode . Upgrade ) . ToList ( ) ;
32
+ using ( session . BeginTransaction ( ) )
33
+ {
34
+ var result = ( from e in db . Customers
35
+ select e ) . Skip ( 5 ) . Take ( 5 ) . SetLockMode ( LockMode . Upgrade ) . ToList ( ) ;
36
+
37
+ Assert . That ( result , Has . Count . EqualTo ( 5 ) ) ;
38
+ Assert . That ( session . GetCurrentLockMode ( result [ 0 ] ) , Is . EqualTo ( LockMode . Upgrade ) ) ;
39
+ AssertSeparateTransactionIsLockedOut ( result [ 0 ] . CustomerId ) ;
40
+ }
29
41
}
30
42
31
43
32
44
[ Test ]
33
45
public void CanLockBeforeSkipOnLinqOrderedPageQuery ( )
34
46
{
35
- var result = ( from e in db . Customers
36
- orderby e . CompanyName
37
- select e )
38
- . SetLockMode ( LockMode . Upgrade ) . Skip ( 5 ) . Take ( 5 ) . ToList ( ) ;
39
-
47
+ using ( session . BeginTransaction ( ) )
48
+ {
49
+ var result = ( from e in db . Customers
50
+ orderby e . CompanyName
51
+ select e )
52
+ . SetLockMode ( LockMode . Upgrade ) . Skip ( 5 ) . Take ( 5 ) . ToList ( ) ;
40
53
54
+ Assert . That ( result , Has . Count . EqualTo ( 5 ) ) ;
55
+ Assert . That ( session . GetCurrentLockMode ( result [ 0 ] ) , Is . EqualTo ( LockMode . Upgrade ) ) ;
56
+ AssertSeparateTransactionIsLockedOut ( result [ 0 ] . CustomerId ) ;
57
+ }
41
58
}
42
59
43
60
61
+ private void AssertSeparateTransactionIsLockedOut ( string customerId )
62
+ {
63
+ using ( new TransactionScope ( TransactionScopeOption . Suppress ) )
64
+ using ( var s2 = OpenSession ( ) )
65
+ using ( s2 . BeginTransaction ( ) )
66
+ {
67
+ // TODO: We should try to verify that the exception actually IS a locking failure and not something unrelated.
68
+ Assert . Throws < GenericADOException > (
69
+ ( ) =>
70
+ {
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." ) ;
77
+ }
78
+ }
44
79
}
45
-
46
80
}
47
81
82
+
0 commit comments