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

SQLite: Make optionsBuilder.CommandTimeout() also set connection.DefaultTimeout #20010

Merged
merged 1 commit into from
Feb 21, 2020
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
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;
bricelam marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
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);
}
}
}