Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit f27c05d

Browse files
committed
Fix Sqlite IsolationLevel.ReadCommited on .NET Core
1 parent 560029e commit f27c05d

9 files changed

+34
-29
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#if NETSTANDARD1_3
2+
using System.Data;
3+
using Microsoft.Data.Sqlite;
4+
5+
namespace ServiceStack.OrmLite.Sqlite
6+
{
7+
public class NetCoreSqliteConnection : SqliteConnection
8+
{
9+
public NetCoreSqliteConnection(string connectionString)
10+
: base(connectionString) {}
11+
12+
public override SqliteTransaction BeginTransaction(IsolationLevel isolationLevel)
13+
{
14+
//.NET Core Sqlite does not support IsolationLevel.ReadCommited
15+
if (isolationLevel == IsolationLevel.ReadCommitted)
16+
isolationLevel = IsolationLevel.Serializable;
17+
18+
return base.BeginTransaction(isolationLevel);
19+
}
20+
}
21+
}
22+
#endif

src/ServiceStack.OrmLite.Sqlite/ServiceStack.OrmLite.Sqlite.Android.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<Compile Include="SqliteOrmLiteDialectProvider.cs" />
6060
<Compile Include="SqliteOrmLiteDialectProviderBase.cs" />
6161
<Compile Include="SqliteExpression.cs" />
62+
<Compile Include="NetCoreSqliteConnection.cs" />
6263
</ItemGroup>
6364
<ItemGroup>
6465
<ProjectReference Include="..\ServiceStack.OrmLite\ServiceStack.OrmLite.Android.csproj">

src/ServiceStack.OrmLite.Sqlite/ServiceStack.OrmLite.Sqlite.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
<Compile Include="SqliteOrmLiteDialectProvider.cs" />
121121
<Compile Include="Properties\AssemblyInfo.cs" />
122122
<Compile Include="SqliteExpression.cs" />
123+
<Compile Include="NetCoreSqliteConnection.cs" />
123124
</ItemGroup>
124125
<ItemGroup>
125126
<ProjectReference Include="..\ServiceStack.OrmLite\ServiceStack.OrmLite.csproj">

src/ServiceStack.OrmLite.Sqlite/ServiceStack.OrmLite.Sqlite.iOS.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<Compile Include="SqliteExpressionVisitor.cs" />
4040
<Compile Include="SqliteOrmLiteDialectProvider.cs" />
4141
<Compile Include="SqliteOrmLiteDialectProviderBase.cs" />
42+
<Compile Include="NetCoreSqliteConnection.cs" />
4243
</ItemGroup>
4344
<ItemGroup>
4445
<ProjectReference Include="..\ServiceStack.OrmLite\ServiceStack.OrmLite.iOS.csproj">

src/ServiceStack.OrmLite.Sqlite/SqliteOrmLiteDialectProvider.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ public class SqliteOrmLiteDialectProvider : SqliteOrmLiteDialectProviderBase
1313

1414
protected override IDbConnection CreateConnection(string connectionString)
1515
{
16+
#if NETSTANDARD1_3
17+
return new NetCoreSqliteConnection(connectionString);
18+
#else
1619
return new SqliteConnection(connectionString);
20+
#endif
1721
}
1822

1923
public override IDbDataParameter CreateParam()

tests/ServiceStack.OrmLite.Tests/Issues/SaveDomainUserReferencesIssue.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,7 @@ public void Can_save_DomainUser_references()
8383
}
8484
};
8585

86-
#if NETCORE
87-
var isolationLevel = base.Dialect == Dialect.Sqlite ? IsolationLevel.Serializable : IsolationLevel.ReadCommitted;
88-
#else
89-
var isolationLevel = IsolationLevel.ReadCommitted;
90-
#endif
91-
92-
using (var trans = db.OpenTransaction(isolationLevel))
86+
using (var trans = db.OpenTransaction(IsolationLevel.ReadCommitted))
9387
{
9488
//Same as below in 1 line
9589
//db.Save(user, references: true);
@@ -113,7 +107,7 @@ public void Can_save_DomainUser_references()
113107
Details = "Reese",
114108
});
115109

116-
using (var trans = db.OpenTransaction(isolationLevel))
110+
using (var trans = db.OpenTransaction(IsolationLevel.ReadCommitted))
117111
{
118112
//Same as below in 1 line
119113
//db.Save(user, references: true);

tests/ServiceStack.OrmLite.Tests/ShippersExample.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,7 @@ public void Shippers_UseCase()
8181
dbTrans.Commit();
8282
}
8383

84-
#if NETCORE
85-
var isolationLevel = base.Dialect == Dialect.Sqlite ? IsolationLevel.Serializable : IsolationLevel.ReadCommitted;
86-
#else
87-
var isolationLevel = IsolationLevel.ReadCommitted;
88-
#endif
89-
90-
using (IDbTransaction dbTrans = db.OpenTransaction(isolationLevel))
84+
using (IDbTransaction dbTrans = db.OpenTransaction(IsolationLevel.ReadCommitted))
9185
{
9286
db.Insert(new ShipperType { Name = "Automobiles" });
9387
Assert.That(db.Select<ShipperType>(), Has.Count.EqualTo(3));

tests/ServiceStack.OrmLite.Tests/UseCase/CustomerOrdersUseCase.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,8 @@ public void Run()
169169
customer = db.Single<Customer>(new { customer.Email }); //Query
170170
Assert.That(customer.Id, Is.EqualTo(customerId));
171171

172-
#if NETCORE
173-
var isolationLevel = base.Dialect == Dialect.Sqlite ? IsolationLevel.Serializable : IsolationLevel.ReadCommitted;
174-
#else
175-
var isolationLevel = IsolationLevel.ReadCommitted;
176-
#endif
177-
178172
//Direct access to System.Data.Transactions:
179-
using (IDbTransaction trans = db.OpenTransaction(isolationLevel))
173+
using (IDbTransaction trans = db.OpenTransaction(IsolationLevel.ReadCommitted))
180174
{
181175
var order = new Order {
182176
CustomerId = customer.Id,

tests/ServiceStack.OrmLite.Tests/UseCase/CustomerOrdersUseCaseAsync.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,8 @@ public async Task Can_run_Customer_Orders_UseCase()
5050
customer = await db.SingleAsync<Customer>(new { customer.Email }); //Query
5151
Assert.That(customer.Id, Is.EqualTo(customerId));
5252

53-
#if NETCORE
54-
var isolationLevel = base.Dialect == Dialect.Sqlite ? IsolationLevel.Serializable : IsolationLevel.ReadCommitted;
55-
#else
56-
var isolationLevel = IsolationLevel.ReadCommitted;
57-
#endif
58-
5953
//Direct access to System.Data.Transactions:
60-
using (IDbTransaction trans = db.OpenTransaction(isolationLevel))
54+
using (IDbTransaction trans = db.OpenTransaction(IsolationLevel.ReadCommitted))
6155
{
6256
var order = new Order {
6357
CustomerId = customer.Id,

0 commit comments

Comments
 (0)