rework connection management

This commit is contained in:
Luke Pulverenti 2016-11-22 00:55:34 -05:00
parent 92cf390fb0
commit 70df357e1e

View File

@ -38,8 +38,6 @@ namespace Emby.Server.Implementations.Data
/// </summary> /// </summary>
public class SqliteItemRepository : BaseSqliteRepository, IItemRepository public class SqliteItemRepository : BaseSqliteRepository, IItemRepository
{ {
private SQLiteDatabaseConnection _connection;
private readonly TypeMapper _typeMapper; private readonly TypeMapper _typeMapper;
/// <summary> /// <summary>
@ -97,13 +95,13 @@ namespace Emby.Server.Implementations.Data
DbFilePath = Path.Combine(_config.ApplicationPaths.DataPath, "library.db"); DbFilePath = Path.Combine(_config.ApplicationPaths.DataPath, "library.db");
} }
protected override bool AllowLockRecursion //protected override bool AllowLockRecursion
{ //{
get // get
{ // {
return true; // return true;
} // }
} //}
private const string ChaptersTableName = "Chapters2"; private const string ChaptersTableName = "Chapters2";
@ -121,15 +119,27 @@ namespace Emby.Server.Implementations.Data
} }
} }
private SQLiteDatabaseConnection _backgroundConnection;
protected override void CloseConnection()
{
base.CloseConnection();
if (_backgroundConnection != null)
{
_backgroundConnection.Dispose();
_backgroundConnection = null;
}
}
/// <summary> /// <summary>
/// Opens the connection to the database /// Opens the connection to the database
/// </summary> /// </summary>
/// <returns>Task.</returns> /// <returns>Task.</returns>
public async Task Initialize(SqliteUserDataRepository userDataRepo) public async Task Initialize(SqliteUserDataRepository userDataRepo)
{ {
_connection = CreateConnection(false); using (var connection = CreateConnection())
{
_connection.ExecuteAll(string.Join(";", new[] connection.ExecuteAll(string.Join(";", new[]
{ {
"PRAGMA page_size=4096", "PRAGMA page_size=4096",
"PRAGMA default_temp_store=memory", "PRAGMA default_temp_store=memory",
@ -164,9 +174,9 @@ namespace Emby.Server.Implementations.Data
}; };
_connection.RunQueries(queries); connection.RunQueries(queries);
_connection.RunInTransaction(db => connection.RunInTransaction(db =>
{ {
var existingColumnNames = GetColumnNames(db, "AncestorIds"); var existingColumnNames = GetColumnNames(db, "AncestorIds");
AddColumn(db, "AncestorIds", "AncestorIdText", "Text", existingColumnNames); AddColumn(db, "AncestorIds", "AncestorIdText", "Text", existingColumnNames);
@ -350,13 +360,17 @@ namespace Emby.Server.Implementations.Data
"create index if not exists idx_ItemValues7 on ItemValues(Type,CleanValue,ItemId)" "create index if not exists idx_ItemValues7 on ItemValues(Type,CleanValue,ItemId)"
}; };
_connection.RunQueries(postQueries); connection.RunQueries(postQueries);
//SqliteExtensions.Attach(_connection, Path.Combine(_config.ApplicationPaths.DataPath, "userdata_v2.db"), "UserDataDb"); //SqliteExtensions.Attach(_connection, Path.Combine(_config.ApplicationPaths.DataPath, "userdata_v2.db"), "UserDataDb");
userDataRepo.Initialize(WriteLock);
//await Vacuum(_connection).ConfigureAwait(false); //await Vacuum(_connection).ConfigureAwait(false);
} }
userDataRepo.Initialize(WriteLock);
_backgroundConnection = CreateConnection(true);
}
private readonly string[] _retriveItemColumns = private readonly string[] _retriveItemColumns =
{ {
"type", "type",
@ -2127,15 +2141,6 @@ namespace Emby.Server.Implementations.Data
} }
} }
protected override void CloseConnection()
{
if (_connection != null)
{
_connection.Dispose();
_connection = null;
}
}
private bool EnableJoinUserData(InternalItemsQuery query) private bool EnableJoinUserData(InternalItemsQuery query)
{ {
if (query.User == null) if (query.User == null)