From 46c819f3142f0a230f50ec5e3ad4b36313a6c119 Mon Sep 17 00:00:00 2001 From: JRahnama Date: Mon, 27 Sep 2021 12:04:55 -0700 Subject: [PATCH 1/4] db_id() issue. --- .../Microsoft/Data/SqlClient/SqlDependencyListener.cs | 11 ++++++++++- .../Microsoft/Data/SqlClient/SqlDependencyListener.cs | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs index 6bf21e5a59..d25dd5753c 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs @@ -90,6 +90,7 @@ internal SqlConnectionContainer(SqlConnectionContainerHashHelper hashHelper, str _con.Open(); _cachedServer = _con.DataSource; + bool? dbId = null; _escapedQueueName = SqlConnection.FixupDatabaseTransactionName(_queue); // Properly escape to prevent SQL Injection. _appDomainKeyHash = new Dictionary(); // Dictionary stores the Start/Stop refcount per AppDomain for this container. @@ -100,7 +101,15 @@ internal SqlConnectionContainer(SqlConnectionContainerHashHelper hashHelper, str CommandText = "select is_broker_enabled from sys.databases where database_id=db_id()" }; - if (!(bool)_com.ExecuteScalar()) + // db_id() returns the database ID of the current database hence it will always be one line result + using SqlDataReader reader = _com.ExecuteReader(CommandBehavior.SingleRow); + while (reader.Read()) + { + dbId = reader.GetBoolean(0); + } + //reader.Close(); + + if (dbId is null || !dbId.Value) { throw SQL.SqlDependencyDatabaseBrokerDisabled(); } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs index 23dba04f58..bb7b55015a 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs @@ -138,11 +138,20 @@ internal SqlConnectionContainer(SqlConnectionContainerHashHelper hashHelper, str _appDomainKeyHash = new Dictionary(); // Dictionary stores the Start/Stop refcount per AppDomain for this container. _com = new SqlCommand(); _com.Connection = _con; + bool? dbId = null; // SQL BU DT 391534 - determine if broker is enabled on current database. _com.CommandText = "select is_broker_enabled from sys.databases where database_id=db_id()"; - if (!(bool)_com.ExecuteScalar()) + // db_id() returns the database ID of the current database hence it will always be one line result + using SqlDataReader reader = _com.ExecuteReader(CommandBehavior.SingleRow); + while (reader.Read()) + { + dbId = reader.GetBoolean(0); + } + reader.Close(); + + if (dbId is null || !dbId.Value) { throw SQL.SqlDependencyDatabaseBrokerDisabled(); } From 6e6c245c943a78617a6da14332350ae1a28b015d Mon Sep 17 00:00:00 2001 From: JRahnama Date: Mon, 27 Sep 2021 12:05:52 -0700 Subject: [PATCH 2/4] closed reader. --- .../src/Microsoft/Data/SqlClient/SqlDependencyListener.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs index d25dd5753c..6304b0ab30 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs @@ -107,7 +107,7 @@ internal SqlConnectionContainer(SqlConnectionContainerHashHelper hashHelper, str { dbId = reader.GetBoolean(0); } - //reader.Close(); + reader.Close(); if (dbId is null || !dbId.Value) { From c4fe6e83fd964cf8bebda0c609e4b02d333f3be7 Mon Sep 17 00:00:00 2001 From: JRahnama Date: Mon, 27 Sep 2021 13:04:45 -0700 Subject: [PATCH 3/4] commit --- .../src/Microsoft/Data/SqlClient/SqlDependencyListener.cs | 2 +- .../netfx/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs index 6304b0ab30..6dbd7475a9 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs @@ -103,7 +103,7 @@ internal SqlConnectionContainer(SqlConnectionContainerHashHelper hashHelper, str // db_id() returns the database ID of the current database hence it will always be one line result using SqlDataReader reader = _com.ExecuteReader(CommandBehavior.SingleRow); - while (reader.Read()) + if (reader.Read() && reader[0] is not null) { dbId = reader.GetBoolean(0); } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs index bb7b55015a..5054367ab1 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs @@ -145,7 +145,7 @@ internal SqlConnectionContainer(SqlConnectionContainerHashHelper hashHelper, str // db_id() returns the database ID of the current database hence it will always be one line result using SqlDataReader reader = _com.ExecuteReader(CommandBehavior.SingleRow); - while (reader.Read()) + if (reader.Read() && reader[0] is not null) { dbId = reader.GetBoolean(0); } From a4651020bf24d7871eafcff93b01df48f45fc725 Mon Sep 17 00:00:00 2001 From: JRahnama Date: Tue, 28 Sep 2021 10:26:50 -0700 Subject: [PATCH 4/4] review comments --- .../Microsoft/Data/SqlClient/SqlDependencyListener.cs | 9 +++++---- .../Microsoft/Data/SqlClient/SqlDependencyListener.cs | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs index 6dbd7475a9..98a87648b4 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs @@ -102,12 +102,13 @@ internal SqlConnectionContainer(SqlConnectionContainerHashHelper hashHelper, str }; // db_id() returns the database ID of the current database hence it will always be one line result - using SqlDataReader reader = _com.ExecuteReader(CommandBehavior.SingleRow); - if (reader.Read() && reader[0] is not null) + using (SqlDataReader reader = _com.ExecuteReader(CommandBehavior.SingleRow)) { - dbId = reader.GetBoolean(0); + if (reader.Read() && reader[0] is not null) + { + dbId = reader.GetBoolean(0); + } } - reader.Close(); if (dbId is null || !dbId.Value) { diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs index 5054367ab1..449ceb0762 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs @@ -144,12 +144,13 @@ internal SqlConnectionContainer(SqlConnectionContainerHashHelper hashHelper, str _com.CommandText = "select is_broker_enabled from sys.databases where database_id=db_id()"; // db_id() returns the database ID of the current database hence it will always be one line result - using SqlDataReader reader = _com.ExecuteReader(CommandBehavior.SingleRow); - if (reader.Read() && reader[0] is not null) + using (SqlDataReader reader = _com.ExecuteReader(CommandBehavior.SingleRow)) { - dbId = reader.GetBoolean(0); + if (reader.Read() && reader[0] is not null) + { + dbId = reader.GetBoolean(0); + } } - reader.Close(); if (dbId is null || !dbId.Value) {