Skip to content

Commit 5ca6954

Browse files
committed
Make sure transactions are correctly crafted.
1 parent 1aaad8e commit 5ca6954

6 files changed

Lines changed: 279 additions & 402 deletions

File tree

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Add this package to a netstandard compatible project:
55
[![NuGet](https://img.shields.io/nuget/v/sqlite-net2.svg)](https://www.nuget.org/packages/sqlite-net2/)
66
![Nuget](https://img.shields.io/nuget/dt/sqlite-net2)
77

8-
**Required**: add ONLY ONE of the followin packages to your common project:
8+
**Required**: add ONLY ONE of the following packages to your common project:
99
- [SQLitePCLRaw.bundle_e_sqlite3](https://www.nuget.org/packages/SQLitePCLRaw.bundle_e_sqlite3) for a normal database file
1010
- [SQLitePCLRaw.bundle_e_sqlcipher](https://www.nuget.org/packages/SQLitePCLRaw.bundle_e_sqlcipher) for an encrypted database file
1111

@@ -49,7 +49,7 @@ For a key/value store based on sqlite, or a drop-in replacement (alternative) to
4949
Usage: `ExecuteSimpleQuery<string>("select 'drop table ' || name || ';' from sqlite_master where type = 'table'")`
5050

5151
* No asynchronous API. Use Task.Run() if you want asynchronous calls.
52-
Note that while SQLitePCLRaw states that the database can be accessed by mutiple threads simultaneously, experience proves that you should always prevent multithread access, otherwise rare random crash occur. You can use `SemaphoreSlim` to serialize calls.
52+
Note that while SQLitePCLRaw states that the database can be accessed by multiple threads simultaneously, experience proves that you should always prevent multithreaded access, otherwise rare random crash occur. You can use `SemaphoreSlim` to serialize calls.
5353

5454
* Another trick
5555
Use transactions! In sqlite they speed up all queries a lot.
@@ -192,3 +192,33 @@ if (String.IsNullOrWhiteSpace(cipherVer))
192192
throw new Exception("This build is not using SQL CIPHER");
193193
```
194194

195+
## Using transactions
196+
197+
Warning: all transactions methods create a state in this connection (the transaction depth).
198+
Be sure to not share the connection with other simultaneous threads.
199+
200+
Especially these methods create by default an implicit transaction:
201+
`InsertAll(), InsertOrUpdateAll(), ReplaceAll()`
202+
They all have a boolean parameter to disable the implicit transaction (beware of performances).
203+
204+
Standard transaction:
205+
```c#
206+
BeginTransaction();
207+
...
208+
Commit();
209+
//or
210+
Rollback();
211+
```
212+
213+
Nested transactions:
214+
```c#
215+
var savepoint=SaveTransactionPoint();
216+
...
217+
Release(savepoint);
218+
//or
219+
RollbackTo(savepoint);
220+
```
221+
222+
## Limitations
223+
224+
Most databases (except SQLServer) store DateTimeOffset as UTC, forgetting the offset part. SQlite is not an exception.

SQLite.Net.Tests/InsertTest.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,7 @@ public void InsertAllFailureOutsideTransaction()
143143
[Test]
144144
public void InsertAllSuccessInsideTransaction()
145145
{
146-
List<UniqueObj> testObjects = Enumerable.Range(1, 20).Select(i => new UniqueObj
147-
{
148-
Id = i
149-
}).ToList();
150-
146+
var testObjects = Enumerable.Range(1, 20).Select(i => new UniqueObj { Id = i }).ToList();
151147
_db.RunInTransaction(() => { _db.InsertAll(testObjects); });
152148

153149
Assert.AreEqual(testObjects.Count, _db.Table<UniqueObj>().Count());

SQLite.Net.Tests/SQLite.Net2.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="nunit" Version="3.13.2" />
10+
<PackageReference Include="nunit" Version="3.13.3" />
1111
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1">
1212
<PrivateAssets>all</PrivateAssets>
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
</PackageReference>
15-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
1616
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.0.7" />
1717
</ItemGroup>
1818

nuget/pack.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ if ($IsMacOS) {
66
$msbuild = & $vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath
77
$msbuild = join-path $msbuild 'MSBuild\Current\Bin\MSBuild.exe'
88
}
9-
$version="2.0.7"
10-
$versionSuffix=""
9+
$version="2.1.0"
10+
$versionSuffix="-pre1"
1111

1212
#####################
1313
#Build release config

src/SQLite.Net/Interop/SQLiteApi.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ public ReadOnlySpan<byte> ColumnByteArray(IDbStatement stmt, int index)
222222
return raw.sqlite3_column_blob(internalStmt.StmtPtr, index);
223223
}
224224

225+
/// <summary>
226+
/// http://www.sqlite.org/c3ref/get_autocommit.html
227+
/// </summary>
228+
public int GetAutoCommit(IDbHandle db)
229+
{
230+
var internalDbHandle = (DbHandle)db;
231+
return raw.sqlite3_get_autocommit(internalDbHandle.DbPtr);
232+
}
233+
225234
#region Backup
226235

227236
public IDbBackupHandle BackupInit(IDbHandle destHandle, string destName, IDbHandle srcHandle, string srcName)

0 commit comments

Comments
 (0)