Merge pull request #7240 from jaysonsantos/add-readonly-connection

This commit is contained in:
Cody Robibero 2022-03-05 13:36:40 -07:00 committed by GitHub
commit bc3cb04c0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 4 deletions

View file

@ -98,8 +98,16 @@ namespace Emby.Server.Implementations.Data
/// <value>The write connection.</value> /// <value>The write connection.</value>
protected SQLiteDatabaseConnection WriteConnection { get; set; } protected SQLiteDatabaseConnection WriteConnection { get; set; }
protected SQLiteDatabaseConnection ReadConnection { get; set; }
protected ManagedConnection GetConnection(bool readOnly = false) protected ManagedConnection GetConnection(bool readOnly = false)
{ {
if (readOnly)
{
ReadConnection ??= SQLite3.Open(DbFilePath, ConnectionFlags.ReadOnly, null);
return new ManagedConnection(ReadConnection, null);
}
WriteLock.Wait(); WriteLock.Wait();
if (WriteConnection != null) if (WriteConnection != null)
{ {

View file

@ -9,13 +9,13 @@ namespace Emby.Server.Implementations.Data
{ {
public sealed class ManagedConnection : IDisposable public sealed class ManagedConnection : IDisposable
{ {
private readonly SemaphoreSlim _writeLock; private readonly SemaphoreSlim? _writeLock;
private SQLiteDatabaseConnection? _db; private SQLiteDatabaseConnection? _db;
private bool _disposed = false; private bool _disposed;
public ManagedConnection(SQLiteDatabaseConnection db, SemaphoreSlim writeLock) public ManagedConnection(SQLiteDatabaseConnection db, SemaphoreSlim? writeLock)
{ {
_db = db; _db = db;
_writeLock = writeLock; _writeLock = writeLock;
@ -73,7 +73,7 @@ namespace Emby.Server.Implementations.Data
return; return;
} }
_writeLock.Release(); _writeLock?.Release();
_db = null; // Don't dispose it _db = null; // Don't dispose it
_disposed = true; _disposed = true;