11using System ;
2- using System . Collections . Concurrent ;
32using System . Collections . Generic ;
43using System . Linq ;
5- using System . Runtime . InteropServices ;
64using System . Threading ;
75using static LiteDB . Constants ;
86
@@ -105,9 +103,10 @@ public TransactionService GetTransaction(bool create, bool queryOnly, out bool i
105103 }
106104
107105 /// <summary>
108- /// Release current thread transaction
106+ /// Dispose and remove transaction from monitor
107+ /// without releasing thread lock
109108 /// </summary>
110- public void ReleaseTransaction ( TransactionService transaction )
109+ public bool RemoveTransaction ( TransactionService transaction )
111110 {
112111 // dispose current transaction
113112 transaction . Dispose ( ) ;
@@ -123,8 +122,16 @@ public void ReleaseTransaction(TransactionService transaction)
123122 _freePages += transaction . MaxTransactionSize ;
124123
125124 // check if current thread contains more query transactions
126- keepLocked = _transactions . Values . Any ( x => x . ThreadID == Environment . CurrentManagedThreadId ) ;
125+ return keepLocked = _transactions . Values . Any ( x => x . ThreadID == Environment . CurrentManagedThreadId ) ;
127126 }
127+ }
128+
129+ /// <summary>
130+ /// Release current thread transaction
131+ /// </summary>
132+ public void ReleaseTransaction ( TransactionService transaction )
133+ {
134+ var keepLocked = RemoveTransaction ( transaction ) ;
128135
129136 // unlock thread-transaction only if there is no more transactions
130137 if ( keepLocked == false )
@@ -150,7 +157,7 @@ public TransactionService GetThreadTransaction()
150157 {
151158 lock ( _transactions )
152159 {
153- return
160+ return
154161 _slot . Value ??
155162 _transactions . Values . FirstOrDefault ( x => x . ThreadID == Environment . CurrentManagedThreadId ) ;
156163 }
@@ -191,7 +198,7 @@ private int GetInitialSize()
191198 /// </summary>
192199 private bool TryExtend ( TransactionService trans )
193200 {
194- lock ( _transactions )
201+ lock ( _transactions )
195202 {
196203 if ( _freePages >= _initialSize )
197204 {
@@ -211,7 +218,7 @@ private bool TryExtend(TransactionService trans)
211218 /// </summary>
212219 public bool CheckSafepoint ( TransactionService trans )
213220 {
214- return
221+ return
215222 trans . Pages . TransactionSize >= trans . MaxTransactionSize &&
216223 this . TryExtend ( trans ) == false ;
217224 }
@@ -232,4 +239,4 @@ public void Dispose()
232239 }
233240 }
234241 }
235- }
242+ }
0 commit comments