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

FIX | db_id returns null on Azure SQL Database. #1294

Merged
merged 5 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -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<string, int>(); // Dictionary stores the Start/Stop refcount per AppDomain for this container.
Expand All @@ -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);
if (reader.Read() && reader[0] is not null)
{
dbId = reader.GetBoolean(0);
}
reader.Close();
JRahnama marked this conversation as resolved.
Show resolved Hide resolved

if (dbId is null || !dbId.Value)
{
throw SQL.SqlDependencyDatabaseBrokerDisabled();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,20 @@ internal SqlConnectionContainer(SqlConnectionContainerHashHelper hashHelper, str
_appDomainKeyHash = new Dictionary<string, int>(); // 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);
if (reader.Read() && reader[0] is not null)
{
dbId = reader.GetBoolean(0);
}
reader.Close();

if (dbId is null || !dbId.Value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add test for code that currently throws NRE?

Copy link
Contributor

@Wraith2 Wraith2 Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be shorter as !dbid.GetValueOrDefault(), I'm not sure if it's clearer and perf isn't a major concern just a general one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the test, it needs more investigation as this only happen on Azure SQL DB and even in this case it may return null.

GetValueOrDefault returns a value even if the HasValue property is false. This also needs more cautious investigation performance wise and also returned value as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetValueOrDefault returns a value even if the HasValue property is false

Yup, and it does so in a single call with a smaller number of instructions than doing the null check then value check. As long as you have that leading negation it would work. But as I said it's hardly a performance problem so leave it as it already is.

{
throw SQL.SqlDependencyDatabaseBrokerDisabled();
}
Expand Down