1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-29 13:25:17 +01:00

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
This commit is contained in:
Matt Gibson 2021-06-25 14:36:59 -04:00 committed by GitHub
parent ee1223b8d3
commit 98415026b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,9 +16,22 @@ namespace Bit.Core.Repositories.SqlServer
{ {
public class OrganizationUserRepository : Repository<OrganizationUser, Guid>, IOrganizationUserRepository 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) public OrganizationUserRepository(GlobalSettings globalSettings)
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString) : this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
{ } {
var builder = new SqlConnectionStringBuilder(ConnectionString)
{
MultipleActiveResultSets = true,
};
_marsConnectionString = builder.ToString();
}
public OrganizationUserRepository(string connectionString, string readOnlyConnectionString) public OrganizationUserRepository(string connectionString, string readOnlyConnectionString)
: base(connectionString, readOnlyConnectionString) : base(connectionString, readOnlyConnectionString)
@ -80,7 +93,7 @@ namespace Bit.Core.Repositories.SqlServer
bool onlyRegisteredUsers) bool onlyRegisteredUsers)
{ {
var emailsTvp = emails.ToArrayTVP("Email"); var emailsTvp = emails.ToArrayTVP("Email");
using (var connection = new SqlConnection(ConnectionString)) using (var connection = new SqlConnection(_marsConnectionString))
{ {
var result = await connection.QueryAsync<string>( var result = await connection.QueryAsync<string>(
"[dbo].[OrganizationUser_SelectKnownEmails]", "[dbo].[OrganizationUser_SelectKnownEmails]",
@ -344,7 +357,7 @@ namespace Bit.Core.Repositories.SqlServer
} }
var orgUsersTVP = organizationUsers.ToTvp(); var orgUsersTVP = organizationUsers.ToTvp();
using (var connection = new SqlConnection(ConnectionString)) using (var connection = new SqlConnection(_marsConnectionString))
{ {
var results = await connection.ExecuteAsync( var results = await connection.ExecuteAsync(
$"[{Schema}].[{Table}_CreateMany]", $"[{Schema}].[{Table}_CreateMany]",
@ -361,7 +374,7 @@ namespace Bit.Core.Repositories.SqlServer
} }
var orgUsersTVP = organizationUsers.ToTvp(); var orgUsersTVP = organizationUsers.ToTvp();
using (var connection = new SqlConnection(ConnectionString)) using (var connection = new SqlConnection(_marsConnectionString))
{ {
var results = await connection.ExecuteAsync( var results = await connection.ExecuteAsync(
$"[{Schema}].[{Table}_UpdateMany]", $"[{Schema}].[{Table}_UpdateMany]",