From 98415026b710153a2443f62eba744736e8274f57 Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Fri, 25 Jun 2021 14:36:59 -0400 Subject: [PATCH] Avoid sql connection timeout (#1414) * Creat TVP prior to opening sql connection Data Table creation is slow. connection may be timing out while we create it. * USe MARS-enabled connections to fix connection issue https://github.com/dotnet/SqlClient/issues/54 --- .../SqlServer/OrganizationUserRepository.cs | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Core/Repositories/SqlServer/OrganizationUserRepository.cs b/src/Core/Repositories/SqlServer/OrganizationUserRepository.cs index 49979ae6b..c204a5c5f 100644 --- a/src/Core/Repositories/SqlServer/OrganizationUserRepository.cs +++ b/src/Core/Repositories/SqlServer/OrganizationUserRepository.cs @@ -16,9 +16,22 @@ namespace Bit.Core.Repositories.SqlServer { public class OrganizationUserRepository : Repository, IOrganizationUserRepository { + /// + /// 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 + /// + 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) @@ -80,7 +93,7 @@ namespace Bit.Core.Repositories.SqlServer bool onlyRegisteredUsers) { var emailsTvp = emails.ToArrayTVP("Email"); - using (var connection = new SqlConnection(ConnectionString)) + using (var connection = new SqlConnection(_marsConnectionString)) { var result = await connection.QueryAsync( "[dbo].[OrganizationUser_SelectKnownEmails]", @@ -344,7 +357,7 @@ namespace Bit.Core.Repositories.SqlServer } var orgUsersTVP = organizationUsers.ToTvp(); - using (var connection = new SqlConnection(ConnectionString)) + using (var connection = new SqlConnection(_marsConnectionString)) { var results = await connection.ExecuteAsync( $"[{Schema}].[{Table}_CreateMany]", @@ -361,7 +374,7 @@ namespace Bit.Core.Repositories.SqlServer } var orgUsersTVP = organizationUsers.ToTvp(); - using (var connection = new SqlConnection(ConnectionString)) + using (var connection = new SqlConnection(_marsConnectionString)) { var results = await connection.ExecuteAsync( $"[{Schema}].[{Table}_UpdateMany]",