update db connections

This commit is contained in:
Luke Pulverenti 2016-12-13 10:44:34 -05:00
parent 71854c1a09
commit 81d685b882
7 changed files with 161 additions and 154 deletions

View File

@ -387,6 +387,7 @@ namespace Emby.Common.Implementations.ScheduledTasks
finally
{
_currentTask = null;
GC.Collect();
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Logging;
@ -43,6 +44,7 @@ namespace Emby.Server.Implementations.Data
//CheckOk(rc);
rc = raw.sqlite3_config(raw.SQLITE_CONFIG_MULTITHREAD, 1);
//rc = raw.sqlite3_config(raw.SQLITE_CONFIG_SINGLETHREAD, 1);
//rc = raw.sqlite3_config(raw.SQLITE_CONFIG_SERIALIZED, 1);
//CheckOk(rc);
@ -54,9 +56,20 @@ namespace Emby.Server.Implementations.Data
private static bool _versionLogged;
private string _defaultWal;
protected ManagedConnection _connection;
protected SQLiteDatabaseConnection CreateConnection(bool isReadOnly = false)
protected virtual bool EnableSingleConnection
{
get { return true; }
}
protected ManagedConnection CreateConnection(bool isReadOnly = false)
{
if (_connection != null)
{
return _connection;
}
lock (WriteLock)
{
if (!_versionLogged)
@ -82,7 +95,15 @@ namespace Emby.Server.Implementations.Data
connectionFlags |= ConnectionFlags.ReadWrite;
}
if (EnableSingleConnection)
{
connectionFlags |= ConnectionFlags.PrivateCache;
}
else
{
connectionFlags |= ConnectionFlags.SharedCached;
}
connectionFlags |= ConnectionFlags.NoMutex;
var db = SQLite3.Open(DbFilePath, connectionFlags, null);
@ -101,6 +122,11 @@ namespace Emby.Server.Implementations.Data
"PRAGMA synchronous=Normal"
};
if (CacheSize.HasValue)
{
queries.Add("PRAGMA cache_size=-" + CacheSize.Value.ToString(CultureInfo.InvariantCulture));
}
if (EnableTempStoreMemory)
{
queries.Add("PRAGMA temp_store = memory");
@ -135,10 +161,22 @@ namespace Emby.Server.Implementations.Data
db.Execute(query);
}
return db;
_connection = new ManagedConnection(db, false);
return _connection;
}
}
public IStatement PrepareStatement(ManagedConnection connection, string sql)
{
return connection.PrepareStatement(sql);
}
public IStatement PrepareStatementSafe(ManagedConnection connection, string sql)
{
return connection.PrepareStatement(sql);
}
public IStatement PrepareStatement(IDatabaseConnection connection, string sql)
{
return connection.PrepareStatement(sql);
@ -159,7 +197,7 @@ namespace Emby.Server.Implementations.Data
return sql.Select(connection.PrepareStatement).ToList();
}
protected void RunDefaultInitialization(IDatabaseConnection db)
protected void RunDefaultInitialization(ManagedConnection db)
{
var queries = new List<string>
{
@ -246,6 +284,12 @@ namespace Emby.Server.Implementations.Data
{
using (WriteLock.Write())
{
if (_connection != null)
{
_connection.Close();
_connection = null;
}
CloseConnection();
}
}
@ -340,7 +384,7 @@ namespace Emby.Server.Implementations.Data
//{
// return new DummyToken();
//}
return new ReadLockToken(obj);
return new WriteLockToken(obj);
}
public static IDisposable Write(this ReaderWriterLockSlim obj)
{

View File

@ -129,7 +129,7 @@ namespace Emby.Server.Implementations.Data
}
}
public static void Attach(IDatabaseConnection db, string path, string alias)
public static void Attach(ManagedConnection db, string path, string alias)
{
var commandText = string.Format("attach @path as {0};", alias);

View File

@ -105,13 +105,7 @@ namespace Emby.Server.Implementations.Data
{
get
{
var cacheSize = _config.Configuration.SqliteCacheSize;
if (cacheSize <= 0)
{
cacheSize = Math.Min(Environment.ProcessorCount * 50000, 100000);
}
return 0 - cacheSize;
return 20000;
}
}
@ -376,9 +370,9 @@ namespace Emby.Server.Implementations.Data
//await Vacuum(_connection).ConfigureAwait(false);
}
userDataRepo.Initialize(WriteLock);
userDataRepo.Initialize(WriteLock, _connection);
_shrinkMemoryTimer = _timerFactory.Create(OnShrinkMemoryTimerCallback, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(30));
_shrinkMemoryTimer = _timerFactory.Create(OnShrinkMemoryTimerCallback, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(15));
}
private void OnShrinkMemoryTimerCallback(object state)
@ -1258,9 +1252,7 @@ namespace Emby.Server.Implementations.Data
{
using (var connection = CreateConnection(true))
{
return connection.RunInTransaction(db =>
{
using (var statement = PrepareStatementSafe(db, "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid"))
using (var statement = PrepareStatementSafe(connection, "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid"))
{
statement.TryBind("@guid", id);
@ -1271,8 +1263,6 @@ namespace Emby.Server.Implementations.Data
}
return null;
}, ReadTransactionMode);
}
}
}
@ -2090,12 +2080,10 @@ namespace Emby.Server.Implementations.Data
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{
return connection.RunInTransaction(db =>
{
var list = new List<ChapterInfo>();
using (var statement = PrepareStatementSafe(db, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc"))
using (var statement = PrepareStatementSafe(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc"))
{
statement.TryBind("@ItemId", id);
@ -2106,8 +2094,6 @@ namespace Emby.Server.Implementations.Data
}
return list;
}, ReadTransactionMode);
}
}
}
@ -2486,9 +2472,7 @@ namespace Emby.Server.Implementations.Data
{
using (var connection = CreateConnection(true))
{
return connection.RunInTransaction(db =>
{
using (var statement = PrepareStatementSafe(db, commandText))
using (var statement = PrepareStatementSafe(connection, commandText))
{
if (EnableJoinUserData(query))
{
@ -2504,8 +2488,6 @@ namespace Emby.Server.Implementations.Data
LogQueryTime("GetCount", commandText, now);
return count;
}
}, ReadTransactionMode);
}
}
@ -2982,12 +2964,10 @@ namespace Emby.Server.Implementations.Data
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{
return connection.RunInTransaction(db =>
{
var list = new List<Guid>();
using (var statement = PrepareStatementSafe(db, commandText))
using (var statement = PrepareStatementSafe(connection, commandText))
{
if (EnableJoinUserData(query))
{
@ -3008,8 +2988,6 @@ namespace Emby.Server.Implementations.Data
LogQueryTime("GetItemList", commandText, now);
return list;
}, ReadTransactionMode);
}
}
}
@ -4677,11 +4655,9 @@ namespace Emby.Server.Implementations.Data
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{
return connection.RunInTransaction(db =>
{
var list = new List<string>();
using (var statement = PrepareStatementSafe(db, commandText))
using (var statement = PrepareStatementSafe(connection, commandText))
{
// Run this again to bind the params
GetPeopleWhereClauses(query, statement);
@ -4692,7 +4668,6 @@ namespace Emby.Server.Implementations.Data
}
}
return list;
}, ReadTransactionMode);
}
}
}
@ -4720,12 +4695,10 @@ namespace Emby.Server.Implementations.Data
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{
return connection.RunInTransaction(db =>
{
var list = new List<PersonInfo>();
using (var statement = PrepareStatementSafe(db, commandText))
using (var statement = PrepareStatementSafe(connection, commandText))
{
// Run this again to bind the params
GetPeopleWhereClauses(query, statement);
@ -4737,7 +4710,6 @@ namespace Emby.Server.Implementations.Data
}
return list;
}, ReadTransactionMode);
}
}
}
@ -4938,12 +4910,10 @@ namespace Emby.Server.Implementations.Data
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{
return connection.RunInTransaction(db =>
{
var list = new List<string>();
using (var statement = PrepareStatementSafe(db, commandText))
using (var statement = PrepareStatementSafe(connection, commandText))
{
foreach (var row in statement.ExecuteQuery())
{
@ -4957,7 +4927,6 @@ namespace Emby.Server.Implementations.Data
LogQueryTime("GetItemValueNames", commandText, now);
return list;
}, ReadTransactionMode);
}
}
}
@ -5433,12 +5402,10 @@ namespace Emby.Server.Implementations.Data
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
{
return connection.RunInTransaction(db =>
{
var list = new List<MediaStream>();
using (var statement = PrepareStatementSafe(db, cmdText))
using (var statement = PrepareStatementSafe(connection, cmdText))
{
statement.TryBind("@ItemId", query.ItemId.ToGuidParamValue());
@ -5459,8 +5426,6 @@ namespace Emby.Server.Implementations.Data
}
return list;
}, ReadTransactionMode);
}
}
}

View File

@ -42,8 +42,10 @@ namespace Emby.Server.Implementations.Data
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
public void Initialize(ReaderWriterLockSlim writeLock)
public void Initialize(ReaderWriterLockSlim writeLock, ManagedConnection managedConnection)
{
_connection = managedConnection;
WriteLock.Dispose();
WriteLock = writeLock;
@ -90,7 +92,7 @@ namespace Emby.Server.Implementations.Data
}
}
private void ImportUserDataIfNeeded(IDatabaseConnection connection)
private void ImportUserDataIfNeeded(ManagedConnection connection)
{
if (!_fileSystem.FileExists(_importFile))
{
@ -117,7 +119,7 @@ namespace Emby.Server.Implementations.Data
}, TransactionMode);
}
private void ImportUserData(IDatabaseConnection connection, string file)
private void ImportUserData(ManagedConnection connection, string file)
{
SqliteExtensions.Attach(connection, file, "UserDataBackup");
@ -300,9 +302,7 @@ namespace Emby.Server.Implementations.Data
{
using (var connection = CreateConnection(true))
{
return connection.RunInTransaction(db =>
{
using (var statement = db.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where key =@Key and userId=@UserId"))
using (var statement = connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where key =@Key and userId=@UserId"))
{
statement.TryBind("@UserId", userId.ToGuidParamValue());
statement.TryBind("@Key", key);
@ -314,8 +314,6 @@ namespace Emby.Server.Implementations.Data
}
return null;
}, ReadTransactionMode);
}
}
}

View File

@ -51,6 +51,7 @@
<Compile Include="Connect\ConnectManager.cs" />
<Compile Include="Connect\Responses.cs" />
<Compile Include="Connect\Validator.cs" />
<Compile Include="Data\ManagedConnection.cs" />
<Compile Include="Data\SqliteDisplayPreferencesRepository.cs" />
<Compile Include="Data\SqliteFileOrganizationRepository.cs" />
<Compile Include="Data\SqliteItemRepository.cs" />

View File

@ -191,7 +191,6 @@ namespace MediaBrowser.Model.Configuration
public int SharingExpirationDays { get; set; }
public int SchemaVersion { get; set; }
public int SqliteCacheSize { get; set; }
public bool EnableAnonymousUsageReporting { get; set; }
public bool EnableStandaloneMusicKeys { get; set; }
@ -214,7 +213,6 @@ namespace MediaBrowser.Model.Configuration
{
LocalNetworkAddresses = new string[] { };
CodecsUsed = new string[] { };
SqliteCacheSize = 0;
ImageExtractionTimeoutMs = 0;
EnableLocalizedGuids = true;