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

Avoid sql connection timeout #1414

Merged
merged 3 commits into from
Jun 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/Core/Repositories/SqlServer/OrganizationUserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,22 @@ namespace Bit.Core.Repositories.SqlServer
{
public class OrganizationUserRepository : Repository<OrganizationUser, Guid>, IOrganizationUserRepository
{
/// <summary>
/// For use with methods with TDS stream issues.
/// This has been observed in Linux-hosted SqlServers with large table-valued-parameters
/// https://github.com/dotnet/SqlClient/issues/54
/// </summary>
private string _marsConnectionString;

public OrganizationUserRepository(GlobalSettings globalSettings)
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
{ }
{
var builder = new SqlConnectionStringBuilder(ConnectionString)
{
MultipleActiveResultSets = true,
};
_marsConnectionString = builder.ToString();
}

public OrganizationUserRepository(string connectionString, string readOnlyConnectionString)
: base(connectionString, readOnlyConnectionString)
Expand Down Expand Up @@ -80,7 +93,7 @@ public async Task<int> GetCountByOrganizationAsync(Guid organizationId, string e
bool onlyRegisteredUsers)
{
var emailsTvp = emails.ToArrayTVP("Email");
using (var connection = new SqlConnection(ConnectionString))
using (var connection = new SqlConnection(_marsConnectionString))
{
var result = await connection.QueryAsync<string>(
"[dbo].[OrganizationUser_SelectKnownEmails]",
Expand Down Expand Up @@ -344,7 +357,7 @@ public async Task CreateManyAsync(IEnumerable<OrganizationUser> organizationUser
}

var orgUsersTVP = organizationUsers.ToTvp();
using (var connection = new SqlConnection(ConnectionString))
using (var connection = new SqlConnection(_marsConnectionString))
{
var results = await connection.ExecuteAsync(
$"[{Schema}].[{Table}_CreateMany]",
Expand All @@ -361,7 +374,7 @@ public async Task ReplaceManyAsync(IEnumerable<OrganizationUser> organizationUse
}

var orgUsersTVP = organizationUsers.ToTvp();
using (var connection = new SqlConnection(ConnectionString))
using (var connection = new SqlConnection(_marsConnectionString))
{
var results = await connection.ExecuteAsync(
$"[{Schema}].[{Table}_UpdateMany]",
Expand Down