Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/6.0] Microsoft.Data.Sqlite: Avoid re-sending PRAGMA key on pooled connections #29215

Merged
merged 1 commit into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions src/Microsoft.Data.Sqlite.Core/SqliteConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,27 +197,6 @@ public override void Open()
_state = ConnectionState.Open;
try
{
if (ConnectionOptions.Password.Length != 0)
{
if (SQLitePCLExtensions.EncryptionSupported(out var libraryName) == false)
{
throw new InvalidOperationException(Resources.EncryptionNotSupported(libraryName));
}

// NB: SQLite doesn't support parameters in PRAGMA statements, so we escape the value using the
// quote function before concatenating.
var quotedPassword = this.ExecuteScalar<string>(
"SELECT quote($password);",
new SqliteParameter("$password", ConnectionOptions.Password));
this.ExecuteNonQuery("PRAGMA key = " + quotedPassword + ";");

if (SQLitePCLExtensions.EncryptionSupported() != false)
{
// NB: Forces decryption. Throws when the key is incorrect.
this.ExecuteNonQuery("SELECT COUNT(*) FROM sqlite_master;");
}
}

if (ConnectionOptions.ForeignKeys.HasValue)
{
this.ExecuteNonQuery(
Expand Down
79 changes: 79 additions & 0 deletions src/Microsoft.Data.Sqlite.Core/SqliteConnectionInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using Microsoft.Data.Sqlite.Properties;
using SQLitePCL;

using static SQLitePCL.raw;
Expand Down Expand Up @@ -94,6 +96,32 @@ public SqliteConnectionInternal(SqliteConnectionStringBuilder connectionOptions,
var rc = sqlite3_open_v2(filename, out _db, flags, vfs: null);
SqliteException.ThrowExceptionForRC(rc, _db);

if (connectionOptions.Password.Length != 0)
{
if (SQLitePCLExtensions.EncryptionSupported(out var libraryName) == false)
{
throw new InvalidOperationException(Resources.EncryptionNotSupported(libraryName));
}

// NB: SQLite doesn't support parameters in PRAGMA statements, so we escape the value using the
// quote function before concatenating.
var quotedPassword = ExecuteScalar(
"SELECT quote($password);",
connectionOptions.Password,
connectionOptions.DefaultTimeout);
ExecuteNonQuery(
"PRAGMA key = " + quotedPassword + ";",
connectionOptions.DefaultTimeout);

if (SQLitePCLExtensions.EncryptionSupported() != false)
{
// NB: Forces decryption. Throws when the key is incorrect.
ExecuteNonQuery(
"SELECT COUNT(*) FROM sqlite_master;",
connectionOptions.DefaultTimeout);
}
}

_pool = pool;
}

Expand Down Expand Up @@ -143,5 +171,56 @@ public void Dispose()
_db.Dispose();
_pool = null;
}

private void ExecuteNonQuery(string sql, int timeout)
=> RetryWhileBusy(() => sqlite3_exec(_db, sql), timeout);

private string ExecuteScalar(string sql, string p1, int timeout)
{
var timer = Stopwatch.StartNew();
sqlite3_stmt stmt = null!;
RetryWhileBusy(() => sqlite3_prepare_v2(_db, sql, out stmt), timeout, timer);
try
{
sqlite3_bind_text(stmt, 1, p1);

RetryWhileBusy(() => sqlite3_step(stmt), () => sqlite3_reset(stmt), timeout, timer);

return sqlite3_column_text(stmt, 0).utf8_to_string();
}
finally
{
stmt.Dispose();
}
}

private void RetryWhileBusy(Func<int> action, int timeout, Stopwatch? timer = null)
=> RetryWhileBusy(action, () => { }, timeout, timer);

private void RetryWhileBusy(Func<int> action, Action reset, int timeout, Stopwatch? timer = null)
{
int rc;
timer ??= Stopwatch.StartNew();

while (IsBusy(rc = action()))
{
if (timeout != 0
&& timer.ElapsedMilliseconds >= timeout * 1000L)
{
break;
}

reset();

Thread.Sleep(150);
}

SqliteException.ThrowExceptionForRC(rc, _db);
}

private static bool IsBusy(int rc)
=> rc == SQLITE_LOCKED
|| rc == SQLITE_BUSY
|| rc == SQLITE_LOCKED_SHAREDCACHE;
}
}