update connection process

This commit is contained in:
Luke Pulverenti 2016-12-13 03:45:04 -05:00
parent e1b880a5a0
commit 71854c1a09
4 changed files with 92 additions and 80 deletions

View file

@ -129,7 +129,7 @@ namespace Emby.Server.Implementations.Activity
var list = new List<ActivityLogEntry>(); var list = new List<ActivityLogEntry>();
var result = new QueryResult<ActivityLogEntry>(); var result = new QueryResult<ActivityLogEntry>();
var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())).ToList(); var statements = PrepareAllSafe(db, statementTexts).ToList();
using (var statement = statements[0]) using (var statement = statements[0])
{ {

View file

@ -25,7 +25,7 @@ namespace Emby.Server.Implementations.Data
protected TransactionMode TransactionMode protected TransactionMode TransactionMode
{ {
get { return TransactionMode.Immediate; } get { return TransactionMode.Deferred; }
} }
protected TransactionMode ReadTransactionMode protected TransactionMode ReadTransactionMode
@ -57,82 +57,86 @@ namespace Emby.Server.Implementations.Data
protected SQLiteDatabaseConnection CreateConnection(bool isReadOnly = false) protected SQLiteDatabaseConnection CreateConnection(bool isReadOnly = false)
{ {
if (!_versionLogged) lock (WriteLock)
{ {
_versionLogged = true; if (!_versionLogged)
Logger.Info("Sqlite version: " + SQLite3.Version); {
Logger.Info("Sqlite compiler options: " + string.Join(",", SQLite3.CompilerOptions.ToArray())); _versionLogged = true;
} Logger.Info("Sqlite version: " + SQLite3.Version);
Logger.Info("Sqlite compiler options: " + string.Join(",", SQLite3.CompilerOptions.ToArray()));
}
ConnectionFlags connectionFlags; ConnectionFlags connectionFlags;
if (isReadOnly) if (isReadOnly)
{ {
//Logger.Info("Opening read connection"); //Logger.Info("Opening read connection");
//connectionFlags = ConnectionFlags.ReadOnly; //connectionFlags = ConnectionFlags.ReadOnly;
connectionFlags = ConnectionFlags.Create; connectionFlags = ConnectionFlags.Create;
connectionFlags |= ConnectionFlags.ReadWrite; connectionFlags |= ConnectionFlags.ReadWrite;
} }
else else
{ {
//Logger.Info("Opening write connection"); //Logger.Info("Opening write connection");
connectionFlags = ConnectionFlags.Create; connectionFlags = ConnectionFlags.Create;
connectionFlags |= ConnectionFlags.ReadWrite; connectionFlags |= ConnectionFlags.ReadWrite;
} }
connectionFlags |= ConnectionFlags.SharedCached; connectionFlags |= ConnectionFlags.SharedCached;
connectionFlags |= ConnectionFlags.NoMutex; connectionFlags |= ConnectionFlags.NoMutex;
var db = SQLite3.Open(DbFilePath, connectionFlags, null); var db = SQLite3.Open(DbFilePath, connectionFlags, null);
if (string.IsNullOrWhiteSpace(_defaultWal)) if (string.IsNullOrWhiteSpace(_defaultWal))
{ {
_defaultWal = db.Query("PRAGMA journal_mode").SelectScalarString().First(); _defaultWal = db.Query("PRAGMA journal_mode").SelectScalarString().First();
Logger.Info("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal); Logger.Info("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal);
} }
var queries = new List<string> var queries = new List<string>
{ {
//"PRAGMA cache size=-10000" //"PRAGMA cache size=-10000"
//"PRAGMA read_uncommitted = true" //"PRAGMA read_uncommitted = true",
"PRAGMA synchronous=Normal"
}; };
if (EnableTempStoreMemory) if (EnableTempStoreMemory)
{
queries.Add("PRAGMA temp_store = memory");
}
//var cacheSize = CacheSize;
//if (cacheSize.HasValue)
//{
//}
////foreach (var query in queries)
////{
//// db.Execute(query);
////}
//Logger.Info("synchronous: " + db.Query("PRAGMA synchronous").SelectScalarString().First());
//Logger.Info("temp_store: " + db.Query("PRAGMA temp_store").SelectScalarString().First());
/*if (!string.Equals(_defaultWal, "wal", StringComparison.OrdinalIgnoreCase))
{
queries.Add("PRAGMA journal_mode=WAL");
using (WriteLock.Write())
{ {
db.ExecuteAll(string.Join(";", queries.ToArray())); queries.Add("PRAGMA temp_store = memory");
} }
}
else*/
if (queries.Count > 0)
{
db.ExecuteAll(string.Join(";", queries.ToArray()));
}
return db; //var cacheSize = CacheSize;
//if (cacheSize.HasValue)
//{
//}
////foreach (var query in queries)
////{
//// db.Execute(query);
////}
//Logger.Info("synchronous: " + db.Query("PRAGMA synchronous").SelectScalarString().First());
//Logger.Info("temp_store: " + db.Query("PRAGMA temp_store").SelectScalarString().First());
/*if (!string.Equals(_defaultWal, "wal", StringComparison.OrdinalIgnoreCase))
{
queries.Add("PRAGMA journal_mode=WAL");
using (WriteLock.Write())
{
db.ExecuteAll(string.Join(";", queries.ToArray()));
}
}
else*/
foreach (var query in queries)
{
db.Execute(query);
}
return db;
}
} }
public IStatement PrepareStatement(IDatabaseConnection connection, string sql) public IStatement PrepareStatement(IDatabaseConnection connection, string sql)
@ -145,14 +149,14 @@ namespace Emby.Server.Implementations.Data
return connection.PrepareStatement(sql); return connection.PrepareStatement(sql);
} }
public List<IStatement> PrepareAll(IDatabaseConnection connection, string sql) public List<IStatement> PrepareAll(IDatabaseConnection connection, IEnumerable<string> sql)
{ {
return connection.PrepareAll(sql).ToList(); return PrepareAllSafe(connection, sql);
} }
public List<IStatement> PrepareAllSafe(IDatabaseConnection connection, string sql) public List<IStatement> PrepareAllSafe(IDatabaseConnection connection, IEnumerable<string> sql)
{ {
return connection.PrepareAll(sql).ToList(); return sql.Select(connection.PrepareStatement).ToList();
} }
protected void RunDefaultInitialization(IDatabaseConnection db) protected void RunDefaultInitialization(IDatabaseConnection db)
@ -161,6 +165,7 @@ namespace Emby.Server.Implementations.Data
{ {
"PRAGMA journal_mode=WAL", "PRAGMA journal_mode=WAL",
"PRAGMA page_size=4096", "PRAGMA page_size=4096",
"PRAGMA synchronous=Normal"
}; };
if (EnableTempStoreMemory) if (EnableTempStoreMemory)
@ -173,6 +178,7 @@ namespace Emby.Server.Implementations.Data
} }
db.ExecuteAll(string.Join(";", queries.ToArray())); db.ExecuteAll(string.Join(";", queries.ToArray()));
Logger.Info("PRAGMA synchronous=" + db.Query("PRAGMA synchronous").SelectScalarString().First());
} }
protected virtual bool EnableTempStoreMemory protected virtual bool EnableTempStoreMemory

View file

@ -701,12 +701,12 @@ namespace Emby.Server.Implementations.Data
{ {
var requiresReset = false; var requiresReset = false;
var statements = PrepareAll(db, string.Join(";", new string[] var statements = PrepareAllSafe(db, new string[]
{ {
GetSaveItemCommandText(), GetSaveItemCommandText(),
"delete from AncestorIds where ItemId=@ItemId", "delete from AncestorIds where ItemId=@ItemId",
"insert into AncestorIds (ItemId, AncestorId, AncestorIdText) values (@ItemId, @AncestorId, @AncestorIdText)" "insert into AncestorIds (ItemId, AncestorId, AncestorIdText) values (@ItemId, @AncestorId, @AncestorIdText)"
})).ToList(); }).ToList();
using (var saveItemStatement = statements[0]) using (var saveItemStatement = statements[0])
{ {
@ -1258,18 +1258,23 @@ namespace Emby.Server.Implementations.Data
{ {
using (var connection = CreateConnection(true)) using (var connection = CreateConnection(true))
{ {
using (var statement = PrepareStatementSafe(connection, "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid")) return connection.RunInTransaction(db =>
{ {
statement.TryBind("@guid", id); using (var statement = PrepareStatementSafe(db, "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid"))
foreach (var row in statement.ExecuteQuery())
{ {
return GetItem(row); statement.TryBind("@guid", id);
foreach (var row in statement.ExecuteQuery())
{
return GetItem(row);
}
} }
}
return null;
}, ReadTransactionMode);
} }
} }
return null;
} }
private BaseItem GetItem(IReadOnlyList<IResultSetValue> reader) private BaseItem GetItem(IReadOnlyList<IResultSetValue> reader)
@ -2762,7 +2767,7 @@ namespace Emby.Server.Implementations.Data
return connection.RunInTransaction(db => return connection.RunInTransaction(db =>
{ {
var result = new QueryResult<BaseItem>(); var result = new QueryResult<BaseItem>();
var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())) var statements = PrepareAllSafe(db, statementTexts)
.ToList(); .ToList();
if (!isReturningZeroItems) if (!isReturningZeroItems)
@ -3173,7 +3178,7 @@ namespace Emby.Server.Implementations.Data
{ {
var result = new QueryResult<Guid>(); var result = new QueryResult<Guid>();
var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())) var statements = PrepareAllSafe(db, statementTexts)
.ToList(); .ToList();
if (!isReturningZeroItems) if (!isReturningZeroItems)
@ -5121,7 +5126,8 @@ namespace Emby.Server.Implementations.Data
var list = new List<Tuple<BaseItem, ItemCounts>>(); var list = new List<Tuple<BaseItem, ItemCounts>>();
var result = new QueryResult<Tuple<BaseItem, ItemCounts>>(); var result = new QueryResult<Tuple<BaseItem, ItemCounts>>();
var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())).ToList(); var statements = PrepareAllSafe(db, statementTexts)
.ToList();
if (!isReturningZeroItems) if (!isReturningZeroItems)
{ {

View file

@ -214,7 +214,7 @@ namespace Emby.Server.Implementations.Security
statementTexts.Add(commandText); statementTexts.Add(commandText);
statementTexts.Add("select count (Id) from AccessTokens" + whereTextWithoutPaging); statementTexts.Add("select count (Id) from AccessTokens" + whereTextWithoutPaging);
var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())) var statements = PrepareAllSafe(db, statementTexts)
.ToList(); .ToList();
using (var statement = statements[0]) using (var statement = statements[0])