Skip to content

Commit d12ee99

Browse files
committed
Merge branch 'master' of https://github.com/mbdavid/LiteDB
2 parents be622b8 + 852adcf commit d12ee99

File tree

4 files changed

+70
-15
lines changed

4 files changed

+70
-15
lines changed

LiteDB.Shell/Commands/Help.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void Execute(StringScanner s, Env env)
2828
// getting all HelpAttributes inside assemblies
2929
var helps = AppDomain.CurrentDomain.GetAssemblies()
3030
.SelectMany(x => x.GetTypes())
31-
.Select(x => CustomAttributeExtensions.GetCustomAttributes(typeof(HelpAttribute), true).FirstOrDefault())
31+
.Select(x => CustomAttributeExtensions.GetCustomAttributes(x, typeof(HelpAttribute), true).FirstOrDefault())
3232
.Where(x => x != null)
3333
.Select(x => x as HelpAttribute)
3434
.ToArray();
@@ -49,4 +49,4 @@ public void Execute(StringScanner s, Env env)
4949
}
5050
}
5151
}
52-
}
52+
}

LiteDB.Tests/Engine/Transactions_Tests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace LiteDB.Tests.Engine
1010
{
11+
using System;
12+
1113
public class Transactions_Tests
1214
{
1315
[Fact]
@@ -222,5 +224,50 @@ public void Test_Transaction_States()
222224
person.Count().Should().Be(20);
223225
}
224226
}
227+
228+
private class BlockingStream : MemoryStream
229+
{
230+
public readonly AutoResetEvent Blocked = new AutoResetEvent(false);
231+
public readonly ManualResetEvent ShouldUnblock = new ManualResetEvent(false);
232+
public bool ShouldBlock;
233+
234+
public override void Write(byte[] buffer, int offset, int count)
235+
{
236+
if (this.ShouldBlock)
237+
{
238+
this.Blocked.Set();
239+
this.ShouldUnblock.WaitOne();
240+
this.Blocked.Reset();
241+
}
242+
base.Write(buffer, offset, count);
243+
}
244+
}
245+
246+
[Fact]
247+
public void Test_Transaction_ReleaseWhenFailToStart()
248+
{
249+
var blockingStream = new BlockingStream();
250+
var db = new LiteDatabase(blockingStream) { Timeout = TimeSpan.FromSeconds(1) };
251+
Thread lockerThread = null;
252+
try
253+
{
254+
lockerThread = new Thread(() =>
255+
{
256+
db.GetCollection<Person>().Insert(new Person());
257+
blockingStream.ShouldBlock = true;
258+
db.Checkpoint();
259+
db.Dispose();
260+
});
261+
lockerThread.Start();
262+
blockingStream.Blocked.WaitOne(1000).Should().BeTrue();
263+
Assert.Throws<LiteException>(() => db.GetCollection<Person>().Insert(new Person())).Message.Should().Contain("timeout");
264+
Assert.Throws<LiteException>(() => db.GetCollection<Person>().Insert(new Person())).Message.Should().Contain("timeout");
265+
}
266+
finally
267+
{
268+
blockingStream.ShouldUnblock.Set();
269+
lockerThread?.Join();
270+
}
271+
}
225272
}
226273
}

LiteDB/Engine/Services/TransactionMonitor.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,21 @@ public TransactionService GetTransaction(bool create, bool queryOnly, out bool i
7373
// enter in lock transaction after release _transaction lock
7474
if (alreadyLock == false)
7575
{
76-
_locker.EnterTransaction();
76+
try
77+
{
78+
_locker.EnterTransaction();
79+
}
80+
catch
81+
{
82+
transaction.Dispose();
83+
lock (_transactions)
84+
{
85+
// return pages
86+
_freePages += transaction.MaxTransactionSize;
87+
_transactions.Remove(transaction.TransactionID);
88+
}
89+
throw;
90+
}
7791
}
7892

7993
// do not store in thread query-only transaction

LiteDB/Utils/Constants.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,8 @@ public static void ENSURE(bool conditional, string message = null)
129129
{
130130
Debug.Fail(message);
131131
}
132-
else
133-
{
134-
throw new Exception("LiteDB ENSURE: " + message);
135-
}
132+
133+
throw new Exception("LiteDB ENSURE: " + message);
136134
}
137135
}
138136

@@ -148,10 +146,8 @@ public static void ENSURE(bool ifTest, bool conditional, string message = null)
148146
{
149147
Debug.Fail(message);
150148
}
151-
else
152-
{
153-
throw new Exception("LiteDB ENSURE: " + message);
154-
}
149+
150+
throw new Exception("LiteDB ENSURE: " + message);
155151
}
156152
}
157153

@@ -168,10 +164,8 @@ public static void DEBUG(bool conditional, string message = null)
168164
{
169165
Debug.Fail(message);
170166
}
171-
else
172-
{
173-
throw new Exception("LiteDB DEBUG: " + message);
174-
}
167+
168+
throw new Exception("LiteDB DEBUG: " + message);
175169
}
176170
}
177171
}

0 commit comments

Comments
 (0)