Skip to content

Commit

Permalink
Fixed multi-instance version of queue creation script.
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymonPobiega committed Jul 13, 2017
1 parent 0260413 commit 0ea01eb
Showing 1 changed file with 45 additions and 31 deletions.
76 changes: 45 additions & 31 deletions src/NServiceBus.SqlServer/Legacy/MultiInstance/LegacySql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,54 @@ namespace NServiceBus.Transport.SQLServer
{
class LegacySql
{
internal const string CreateQueueText = @"IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'{0}') AND type in (N'U'))
BEGIN
EXEC sp_getapplock @Resource = '{0}_lock', @LockMode = 'Exclusive'
public static readonly string CreateQueueText = @"
IF EXISTS (
SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'{0}')
AND type in (N'U'))
RETURN
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'{0}') AND type in (N'U'))
BEGIN
CREATE TABLE {0}(
[Id] [uniqueidentifier] NOT NULL,
[CorrelationId] [varchar](255),
[ReplyToAddress] [varchar](255),
[Recoverable] [bit] NOT NULL,
[Expires] [datetime],
[Headers] [varchar](max) NOT NULL,
[Body] [varbinary](max),
[RowVersion] [bigint] IDENTITY(1,1) NOT NULL
);
EXEC sp_getapplock @Resource = '{0}_lock', @LockMode = 'Exclusive'
CREATE CLUSTERED INDEX [Index_RowVersion] ON {0}
(
[RowVersion]
)
IF EXISTS (
SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'{0}')
AND type in (N'U'))
BEGIN
EXEC sp_releaseapplock @Resource = '{0}_lock'
RETURN
END
CREATE NONCLUSTERED INDEX [Index_Expires] ON {0}
(
[Expires]
)
INCLUDE
(
[Id],
[RowVersion]
)
END
CREATE TABLE {0} (

This comment has been minimized.

Copy link
@weralabaj

weralabaj Jul 13, 2017

Contributor

Any reason to remove square brackets? Or just refactoring?

This comment has been minimized.

Copy link
@SzymonPobiega

SzymonPobiega Jul 13, 2017

Author Member

To align with SqlConstants. I actually copied the code over from there and removed the {0}. parts from sys.objects reference. @SimonCropp doesn't like square brackets 😂

Id uniqueidentifier NOT NULL,
CorrelationId varchar(255),
ReplyToAddress varchar(255),
Recoverable bit NOT NULL,
Expires datetime,
Headers nvarchar(max) NOT NULL,
Body varbinary(max),
RowVersion bigint IDENTITY(1,1) NOT NULL
);
EXEC sp_releaseapplock @Resource = '{0}_lock'
END";
CREATE CLUSTERED INDEX Index_RowVersion ON {0}
(
RowVersion
)
CREATE NONCLUSTERED INDEX Index_Expires ON {0}
(
Expires
)
INCLUDE
(
Id,
RowVersion
)
WHERE

This comment has been minimized.

Copy link
@weralabaj

weralabaj Jul 13, 2017

Contributor

Why we needed to add this condition?

This comment has been minimized.

Copy link
@SzymonPobiega

SzymonPobiega Jul 13, 2017

Author Member

It was added by @MarcinHoppe in order to not update the index if the message has no TTBR header.

This comment has been minimized.

Copy link
@MarcinHoppe

MarcinHoppe Jul 13, 2017

Contributor

Exactly, the idea was to eliminate updating the index during send and receive operations for non-TTBR messages.

Expires IS NOT NULL
EXEC sp_releaseapplock @Resource = '{0}_lock'";
}
}

2 comments on commit 0ea01eb

@weralabaj
Copy link
Contributor

Choose a reason for hiding this comment

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

Two small questions :)

@weralabaj
Copy link
Contributor

Choose a reason for hiding this comment

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

OK thanks, LGTM

Please sign in to comment.