aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Data/ManagedConnection.cs
diff options
context:
space:
mode:
authorBond-009 <bond.009@outlook.com>2024-06-06 14:30:40 -0400
committerJoshua M. Boniface <joshua@boniface.me>2024-06-06 14:30:40 -0400
commitb4e32a5ede6807849d618966f92a61a765b80180 (patch)
treee64c86ddd03b24c5dfaa87322be4831056321e0a /Emby.Server.Implementations/Data/ManagedConnection.cs
parent4015ac78cd27881a26a62a862ed775955ba72f1b (diff)
Backport pull request #11986 from jellyfin/release-10.9.z
Use only 1 write connection/DB Original-merge: cc4563a4779ff7e86526b09f5ac5a2e7ec71e56b Merged-by: crobibero <cody@robibe.ro> Backported-by: Joshua M. Boniface <joshua@boniface.me>
Diffstat (limited to 'Emby.Server.Implementations/Data/ManagedConnection.cs')
-rw-r--r--Emby.Server.Implementations/Data/ManagedConnection.cs62
1 files changed, 62 insertions, 0 deletions
diff --git a/Emby.Server.Implementations/Data/ManagedConnection.cs b/Emby.Server.Implementations/Data/ManagedConnection.cs
new file mode 100644
index 000000000..860950b30
--- /dev/null
+++ b/Emby.Server.Implementations/Data/ManagedConnection.cs
@@ -0,0 +1,62 @@
+#pragma warning disable CS1591
+
+using System;
+using System.Collections.Generic;
+using System.Threading;
+using Microsoft.Data.Sqlite;
+
+namespace Emby.Server.Implementations.Data;
+
+public sealed class ManagedConnection : IDisposable
+{
+ private readonly SemaphoreSlim? _writeLock;
+
+ private SqliteConnection _db;
+
+ private bool _disposed = false;
+
+ public ManagedConnection(SqliteConnection db, SemaphoreSlim? writeLock)
+ {
+ _db = db;
+ _writeLock = writeLock;
+ }
+
+ public SqliteTransaction BeginTransaction()
+ => _db.BeginTransaction();
+
+ public SqliteCommand CreateCommand()
+ => _db.CreateCommand();
+
+ public void Execute(string commandText)
+ => _db.Execute(commandText);
+
+ public SqliteCommand PrepareStatement(string sql)
+ => _db.PrepareStatement(sql);
+
+ public IEnumerable<SqliteDataReader> Query(string commandText)
+ => _db.Query(commandText);
+
+ public void Dispose()
+ {
+ if (_disposed)
+ {
+ return;
+ }
+
+ if (_writeLock is null)
+ {
+ // Read connections are managed with an internal pool
+ _db.Dispose();
+ }
+ else
+ {
+ // Write lock is managed by BaseSqliteRepository
+ // Don't dispose here
+ _writeLock.Release();
+ }
+
+ _db = null!;
+
+ _disposed = true;
+ }
+}