Skip to content

Commit

Permalink
SQLite: Make SqliteDbContextOptionsBuilder.CommandTimeout() also set …
Browse files Browse the repository at this point in the history
…SqliteConnection.DefaultTimeout

Fixes #18607
  • Loading branch information
bricelam committed Feb 21, 2020
1 parent 61c7ebc commit 1700908
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class SqliteRelationalConnection : RelationalConnection, ISqliteRelationa
{
private readonly IRawSqlCommandBuilder _rawSqlCommandBuilder;
private readonly bool _loadSpatialite;
private readonly int? _commandTimeout;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -51,14 +52,13 @@ public SqliteRelationalConnection(
if (optionsExtension != null)
{
_loadSpatialite = optionsExtension.LoadSpatialite;
if (_loadSpatialite)

var relationalOptions = RelationalOptionsExtension.Extract(dependencies.ContextOptions);
_commandTimeout = relationalOptions.CommandTimeout;

if (relationalOptions.Connection != null)
{
var relationalOptions = RelationalOptionsExtension.Extract(dependencies.ContextOptions);
if (relationalOptions.Connection != null)
{
// TODO: Provide a better hook to do this for both external connections and ones created by EF
SpatialiteLoader.Load(relationalOptions.Connection);
}
InitializeDbConnection(relationalOptions.Connection);
}
}
}
Expand All @@ -72,11 +72,7 @@ public SqliteRelationalConnection(
protected override DbConnection CreateDbConnection()
{
var connection = new SqliteConnection(GetCheckedConnectionString());

if (_loadSpatialite)
{
SpatialiteLoader.Load(connection);
}
InitializeDbConnection(connection);

return connection;
}
Expand All @@ -95,5 +91,19 @@ public virtual ISqliteRelationalConnection CreateReadOnlyConnection()

return new SqliteRelationalConnection(Dependencies.With(contextOptions), _rawSqlCommandBuilder);
}

private void InitializeDbConnection(DbConnection connection)
{
if (_loadSpatialite)
{
SpatialiteLoader.Load(connection);
}

if (connection is SqliteConnection sqliteConnection
&& _commandTimeout.HasValue)
{
sqliteConnection.DefaultTimeout = _commandTimeout.Value;
}
}
}
}
44 changes: 44 additions & 0 deletions test/EFCore.Sqlite.Tests/Storage/SqliteRelationalConnectionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Microsoft.EntityFrameworkCore.Storage
{
public class SqliteRelationalConnectionTest
{
[Fact]
public void Sets_DefaultTimeout_when_connectionString()
{
var services = SqliteTestHelpers.Instance.CreateContextServices(
new DbContextOptionsBuilder()
.UseSqlite("Data Source=:memory:", x => x.CommandTimeout(42))
.Options);

var connection = (SqliteConnection)services.GetRequiredService<IRelationalConnection>().DbConnection;

Assert.Equal(42, connection.DefaultTimeout);
}

[Fact]
public void Sets_DefaultTimeout_when_connection()
{
var originalConnection = new SqliteConnection("Data Source=:memory:")
{
DefaultTimeout = 21
};
var services = SqliteTestHelpers.Instance.CreateContextServices(
new DbContextOptionsBuilder()
.UseSqlite(originalConnection, x => x.CommandTimeout(42))
.Options);

var connection = (SqliteConnection)services.GetRequiredService<IRelationalConnection>().DbConnection;

Assert.Same(originalConnection, connection);
Assert.Equal(42, originalConnection.DefaultTimeout);
}
}
}

0 comments on commit 1700908

Please sign in to comment.