aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Emby.Server.Implementations/Activity/ActivityRepository.cs1
-rw-r--r--Emby.Server.Implementations/Data/BaseSqliteRepository.cs80
-rw-r--r--Emby.Server.Implementations/Data/ManagedConnection.cs13
-rw-r--r--Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs1
-rw-r--r--Emby.Server.Implementations/Data/SqliteItemRepository.cs1
-rw-r--r--Emby.Server.Implementations/Data/SqliteUserDataRepository.cs1
-rw-r--r--Emby.Server.Implementations/Data/SqliteUserRepository.cs4
-rw-r--r--Emby.Server.Implementations/Security/AuthenticationRepository.cs1
-rw-r--r--MediaBrowser.Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs1
-rw-r--r--MediaBrowser.Api/Session/SessionInfoWebSocketListener.cs1
10 files changed, 23 insertions, 81 deletions
diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs
index a38cb38d7..63931e134 100644
--- a/Emby.Server.Implementations/Activity/ActivityRepository.cs
+++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs
@@ -43,7 +43,6 @@ namespace Emby.Server.Implementations.Activity
private void InitializeInternal()
{
- CreateConnections().GetAwaiter().GetResult();
using (var connection = GetConnection())
{
RunDefaultInitialization(connection);
diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs
index db63f68d0..33a0b7ddf 100644
--- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs
+++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs
@@ -1,9 +1,7 @@
using System;
-using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
-using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using SQLitePCL;
using SQLitePCL.pretty;
@@ -33,8 +31,6 @@ namespace Emby.Server.Implementations.Data
private SQLiteDatabaseConnection WriteConnection;
- private readonly BlockingCollection<SQLiteDatabaseConnection> ReadConnectionPool = new BlockingCollection<SQLiteDatabaseConnection>();
-
static BaseSqliteRepository()
{
ThreadSafeMode = raw.sqlite3_threadsafe();
@@ -43,70 +39,37 @@ namespace Emby.Server.Implementations.Data
private string _defaultWal;
- protected async Task CreateConnections()
+ protected ManagedConnection GetConnection(bool isReadOnly = false)
{
- await WriteLock.WaitAsync().ConfigureAwait(false);
-
- try
+ WriteLock.Wait();
+ if (WriteConnection != null)
{
- if (WriteConnection == null)
- {
- WriteConnection = SQLite3.Open(
- DbFilePath,
- DefaultConnectionFlags | ConnectionFlags.Create | ConnectionFlags.ReadWrite,
- null);
- }
+ return new ManagedConnection(WriteConnection, WriteLock);
+ }
- if (string.IsNullOrWhiteSpace(_defaultWal))
- {
- _defaultWal = WriteConnection.Query("PRAGMA journal_mode").SelectScalarString().First();
+ WriteConnection = SQLite3.Open(
+ DbFilePath,
+ DefaultConnectionFlags | ConnectionFlags.Create | ConnectionFlags.ReadWrite,
+ null);
- Logger.LogInformation("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal);
- }
- if (EnableTempStoreMemory)
- {
- WriteConnection.Execute("PRAGMA temp_store = memory");
- }
- else
- {
- WriteConnection.Execute("PRAGMA temp_store = file");
- }
- }
- catch
+ if (string.IsNullOrWhiteSpace(_defaultWal))
{
+ _defaultWal = WriteConnection.Query("PRAGMA journal_mode").SelectScalarString().First();
- throw;
- }
- finally
- {
- WriteLock.Release();
+ Logger.LogInformation("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal);
}
- // Add one reading connection for each thread
- int threads = System.Environment.ProcessorCount;
- for (int i = 0; i <= threads; i++)
- {
- ReadConnectionPool.Add(SQLite3.Open(DbFilePath, DefaultConnectionFlags | ConnectionFlags.ReadOnly, null));
- }
- }
-
- protected ManagedConnection GetConnection(bool isReadOnly = false)
- {
- if (isReadOnly)
+ if (EnableTempStoreMemory)
{
- return new ManagedConnection(ReadConnectionPool.Take(), ReadConnectionPool);
+ WriteConnection.Execute("PRAGMA temp_store = memory");
}
else
{
- if (WriteConnection == null)
- {
- throw new InvalidOperationException("Can't access the write connection at this time.");
- }
-
- WriteLock.Wait();
- return new ManagedConnection(WriteConnection, WriteLock);
+ WriteConnection.Execute("PRAGMA temp_store = file");
}
+
+ return new ManagedConnection(WriteConnection, WriteLock);
}
public IStatement PrepareStatement(ManagedConnection connection, string sql)
@@ -217,14 +180,11 @@ namespace Emby.Server.Implementations.Data
WriteLock.Release();
}
- foreach (var i in ReadConnectionPool)
- {
- i.Dispose();
- }
-
- ReadConnectionPool.Dispose();
+ WriteLock.Dispose();
}
+ WriteConnection = null;
+
_disposed = true;
}
diff --git a/Emby.Server.Implementations/Data/ManagedConnection.cs b/Emby.Server.Implementations/Data/ManagedConnection.cs
index 71b934a9b..4c3424410 100644
--- a/Emby.Server.Implementations/Data/ManagedConnection.cs
+++ b/Emby.Server.Implementations/Data/ManagedConnection.cs
@@ -1,5 +1,4 @@
using System;
-using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using SQLitePCL.pretty;
@@ -9,8 +8,7 @@ namespace Emby.Server.Implementations.Data
public class ManagedConnection : IDisposable
{
private SQLiteDatabaseConnection _db;
- private SemaphoreSlim _writeLock;
- private BlockingCollection<SQLiteDatabaseConnection> _readConPool;
+ private readonly SemaphoreSlim _writeLock;
private bool _disposed = false;
public ManagedConnection(SQLiteDatabaseConnection db, SemaphoreSlim writeLock)
@@ -19,12 +17,6 @@ namespace Emby.Server.Implementations.Data
_writeLock = writeLock;
}
- public ManagedConnection(SQLiteDatabaseConnection db, BlockingCollection<SQLiteDatabaseConnection> queue)
- {
- _db = db;
- _readConPool = queue;
- }
-
public IStatement PrepareStatement(string sql)
{
return _db.PrepareStatement(sql);
@@ -77,8 +69,7 @@ namespace Emby.Server.Implementations.Data
return;
}
- _writeLock?.Release();
- _readConPool?.Add(_db);
+ _writeLock.Release();
_db = null; // Don't dispose it
_disposed = true;
diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs
index d620f3962..7f8df7626 100644
--- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs
+++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs
@@ -61,7 +61,6 @@ namespace Emby.Server.Implementations.Data
/// <returns>Task.</returns>
private void InitializeInternal()
{
- CreateConnections().GetAwaiter().GetResult();
using (var connection = GetConnection())
{
RunDefaultInitialization(connection);
diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs
index a2f490c4a..9e96d7745 100644
--- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs
+++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs
@@ -99,7 +99,6 @@ namespace Emby.Server.Implementations.Data
/// </summary>
public void Initialize(SqliteUserDataRepository userDataRepo, IUserManager userManager)
{
- CreateConnections().GetAwaiter().GetResult();
using (var connection = GetConnection())
{
RunDefaultInitialization(connection);
diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs
index 9dc31d597..355755014 100644
--- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs
+++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs
@@ -34,7 +34,6 @@ namespace Emby.Server.Implementations.Data
/// <returns>Task.</returns>
public void Initialize(IUserManager userManager)
{
- CreateConnections().GetAwaiter().GetResult();
using (var connection = GetConnection())
{
var userDatasTableExists = TableExists(connection, "UserDatas");
diff --git a/Emby.Server.Implementations/Data/SqliteUserRepository.cs b/Emby.Server.Implementations/Data/SqliteUserRepository.cs
index ef8ae60b3..e79b3d601 100644
--- a/Emby.Server.Implementations/Data/SqliteUserRepository.cs
+++ b/Emby.Server.Implementations/Data/SqliteUserRepository.cs
@@ -40,7 +40,6 @@ namespace Emby.Server.Implementations.Data
/// <returns>Task.</returns>
public void Initialize()
{
- CreateConnections().GetAwaiter().GetResult();
using (var connection = GetConnection())
{
RunDefaultInitialization(connection);
@@ -90,8 +89,7 @@ namespace Emby.Server.Implementations.Data
user.Password = null;
var serialized = _jsonSerializer.SerializeToBytes(user);
- using (WriteLock.Write())
- using (var connection = CreateConnection())
+ using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs
index dfcd6af0d..efe56c081 100644
--- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs
+++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs
@@ -23,7 +23,6 @@ namespace Emby.Server.Implementations.Security
public void Initialize()
{
- CreateConnections().GetAwaiter().GetResult();
using (var connection = GetConnection())
{
RunDefaultInitialization(connection);
diff --git a/MediaBrowser.Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs b/MediaBrowser.Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs
index d24a18743..d9530ffb7 100644
--- a/MediaBrowser.Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs
+++ b/MediaBrowser.Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Linq;
-using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Events;
diff --git a/MediaBrowser.Api/Session/SessionInfoWebSocketListener.cs b/MediaBrowser.Api/Session/SessionInfoWebSocketListener.cs
index b79e9f84b..f1a6622fb 100644
--- a/MediaBrowser.Api/Session/SessionInfoWebSocketListener.cs
+++ b/MediaBrowser.Api/Session/SessionInfoWebSocketListener.cs
@@ -1,5 +1,4 @@
using System.Collections.Generic;
-using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;