2
0

ManagedConnection.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using Microsoft.Data.Sqlite;
  6. namespace Emby.Server.Implementations.Data;
  7. public sealed class ManagedConnection : IDisposable
  8. {
  9. private readonly SemaphoreSlim? _writeLock;
  10. private SqliteConnection _db;
  11. private bool _disposed = false;
  12. public ManagedConnection(SqliteConnection db, SemaphoreSlim? writeLock)
  13. {
  14. _db = db;
  15. _writeLock = writeLock;
  16. }
  17. public SqliteTransaction BeginTransaction()
  18. => _db.BeginTransaction();
  19. public SqliteCommand CreateCommand()
  20. => _db.CreateCommand();
  21. public void Execute(string commandText)
  22. => _db.Execute(commandText);
  23. public SqliteCommand PrepareStatement(string sql)
  24. => _db.PrepareStatement(sql);
  25. public IEnumerable<SqliteDataReader> Query(string commandText)
  26. => _db.Query(commandText);
  27. public void Dispose()
  28. {
  29. if (_disposed)
  30. {
  31. return;
  32. }
  33. if (_writeLock is null)
  34. {
  35. // Read connections are managed with an internal pool
  36. _db.Dispose();
  37. }
  38. else
  39. {
  40. // Write lock is managed by BaseSqliteRepository
  41. // Don't dispose here
  42. _writeLock.Release();
  43. }
  44. _db = null!;
  45. _disposed = true;
  46. }
  47. }