Quantcast
Channel: SQL Service Broker forum
Viewing all 461 articles
Browse latest View live

SEND ON CONVERSATION Appears to Block

$
0
0

Hi

I am sending messages to a remote server using SSB. The messages are sometimes generated at the rate of, say, 10 per second. For each message, my application creates a separate thread, and calls a sproc to send the message.

The sproc assembles a message and then finishes with this

 declare @h uniqueidentifier

 set @h = (select top 1 conversation_handle
     from sys.conversation_endpoints ce
     join sys.service_contracts sc
     on sc.service_contract_id = ce.service_contract_id
     where sc.name = 'MyContract')

 IF @h IS null
 BEGIN
  BEGIN DIALOG CONVERSATION @h
  FROM SERVICE MyService
  TO SERVICE 'MyService'
  ON CONTRACT MyContract
  WITH ENCRYPTION=OFF
 END

 ;SEND ON CONVERSATION @h
 MESSAGE TYPE MyMessage
 (@xmlmessage)

What I am finding is that the SEND takes a finite time to complete, and as each other thread tries to execute this statement it has to wait. The number of waiting threads increases until I get an avalanche effect, with more connections being opened to the database and eventually the connection pool is exhausted, and it all starts to unravel.

Although multiple threads are spawned to deal with the incoming messages, I want to send on a single conversation to maintain ordering, and to avoid the overhead of having to manage multiple conversations. I'm also conscious of the increased overhead at both ends of creating and tearing down conversations.

Firstly, can anyone confirm that what I am seeing is likely to be caused by the SEND statement locking the conversation handle, causing the following threads to wait?

But, perhaps more importantly, can anyone suggest a solution to this?

Thanks

Charles


Differences in XP_CmdShell executions through Service Broker between 2005 and R2

$
0
0
We are upgrading our system from SQL Server 2005 to R2.

We have small module which executes XP_CmdShell through Service Broker (using the Procedure_Name option in the Queue deffinition).

In 2005 it works fine, but in R2 it fails if I don't define a Credential for the XP_CmdShell Proxy (##xp_cmdshell_proxy_account##' credential).

I tried to override it using 'Execute As' with different Logins (me and other sysadmins) but it fails, though the XP_Cmdshell work fine through the SSMS with or without 'Execute As'.

A solution exists: I should use sp_xp_cmdshell_proxy_account system procedure to create a proxy, but I still don't understand why the XP_CmdShell doesn't behave inside the Service Broker in the same way as it behave outside (in the SSMS);
and why this problem doesn't occur in 2005.

Any suggestions?
(this is a revised version of a previous question in the Security Forum)

Geri Reshef http://gerireshef.wordpress.com

Service Broker queue getting disabled automatically

$
0
0

Hello,

I have created one stored procedure which accepts one input parameter. I want to call it asynchronously using Service Broker.

I have implemented all code with the help of below link:

http://rusanu.com/2009/08/18/passing-parameters-to-a-background-procedure/

If I run the example stored procedure which is given in to above link, it runs fine.

But when I run it for my stored procedure it disables the queue.

I tried to enable queue again :

ALTER QUEUE [queuename] WITH STATUS = ON;

Also I tried to execute activated stored procedure using dbo user. I checked logs as well to find the error which makes queue disabled automatically.

use myDB
go

execute as user = 'dbo';
go

exec usp_AsyncExecActivated
go

revert;
go

When I checked server logs it shows below error message.

Error: 50000, Unrecoverable error in procedure: 10772: The operation 'savepoint' is not supported with natively compiled stored procedures.
Msg 50000, Level 1, State 60

When I searched for this error, I found that 'savepoint' operation is not supported by Natively compiled stored procedure. I am using Memory optimized table in my stored procedure.

Is there any work around for this operation ? As I have to use memory optimized table as per client's requirement.


Thank you,

Mittal Patel.






Delay message processing until after a given datetime?

$
0
0

Using Service Broker, is it possible to delay a given message from being processed until some future date/time has gone by?

scott

How to handle EndDialog message when using Asynchronous Trigger Pattern?

$
0
0

I have Service A which sends a message to Service B when a update/delete/insert trigger fires.  Service B dequeing logic looks like:

declare @messagebody xml
	declare @messagetype nvarchar(256)
	declare @cg uniqueidentifier
	declare @ch uniqueidentifier

	DECLARE @messages TABLE(messagetype nvarchar(256),messagebody xml)

	begin try
		begin transaction;
			waitfor
			(
				receive top (1)
					@cg = conversation_group_id,
					@ch = conversation_handle,
					@messagetype = message_type_name,
					@messagebody = cast(message_body as xml)
				from MyPolicyQueue
			),TIMEOUT @receiveTimeoutMs

			if @messagebody is not null
			begin
				insert into @messages values (@messagetype, @messagebody)
			end

			select * from @messages
			end conversation @ch
		commit
	end try

ServiceB will end the conversation but afterwards, the queue now has an EndDialog message that never gets processed.

The intent is asynchronous handling of update messages with the trigger used as the event source.  Service B closes the conversation but trigger does not (It would probably be a very bad thing if trigger blocked waiting for EndDialog message response).  How do you handle the EndDialog message in this scenario?

The code is based on this example.

scott

Monitor open conversations?

$
0
0

Is there a good SQL view for monitoring open service broker conversations ?

scott

Receive message by service name?

$
0
0

Is it possible to receive the next available message for a given service name?

Is it possible to peek at a message but not de-queue it if not applicable to the current service and move on to the next message?

scott

Safe way to terminate a blocking RECEIVE?

$
0
0

I can configure my windows service to call TSQL Receive with an infinite timeout (using SqlCommand / ADO.NET).  Is there a recommended / safe way of breaking out of a RECEIVE that is waiting indefinitely (close the SqlConnection? Thread.Abort maybe)?

scott


closed endpoints in sys.conversation_endpoints?

$
0
0

Do closed endpoints in sys.conversation_endpoints ever go away?  I have several endpoints with a CLOSED state_desc value but they still display in the view several minutes after having been closed.

scott

sql2014

The Database Mirroring endpoint is in disabled or stopped state.

$
0
0

Hello

I'm trying to configure 2 service broker instances to communicate over LAN network.

Right now both sql instances is able to receive messages locally when sent locally.


But as soon as I create an endpoint on SRV1 and route on SRV2

I'm getting this very annoying transmission status: "The Database Mirroring endpoint is in disabled or stopped state."

when running: select transmission_status, * from sys.transmission_queue

My endpoint on "SRV1"-instance:

create endpoint NotificationServiceEndpoint
state = started
as tcp
(
listener_port = 4022
)
for service_broker
(
authentication = certificate NotificationServiceCertPrivate,
encryption = disabled
)

Route on SRV2:

create route NotificationServiceRoute
with service_name = 'NotificationService',
broker_instance = '8EB69F6A-4B30-4C9A-B006-495104C5D75B',
    ADDRESS = 'TCP://192.168.98.30:4022' 

I know 4022 is default, but I've tried many different ports without luck.

On SRV1, if I run: select * from sys.service_broker_endpoints

I get:

Dedicated Admin Connection    1    1    2    TCP    2    TSQL    0    STARTED    1
TSQL Local Machine    2    1    4    SHARED_MEMORY    2    TSQL    0    STARTED    0
TSQL Named Pipes    3    1    3    NAMED_PIPES    2    TSQL    0    STARTED    0
TSQL Default TCP    4    1    2    TCP    2    TSQL    0    STARTED    0
TSQL Default VIA    5    1    5    VIA    2    TSQL    0    STARTED    0
NotificationServiceEndpoint    65544    1    2    TCP    3    SERVICE_BROKER    0    STARTED    0

At first I thought that Mirroring was disabled on some ports, that I unluckily had configured in ServiceBroker but no.


Question on Internal Activation Stored Procedure Security Context

$
0
0
 

CLR function has the following few lines which is invoked from Internal Activation Stored Procedure:

SqlCommand command = Connection.CreateCommand();

command.CommandText = "CREATE ASSEMBLY " + "\"" + AsmName + "\"" +" AUTHORIZATION [dbo]"+ " FROM " + "'" + regasm.UncPath + "'" + " WITH PERMISSION_SET=SAFE";

command.ExecuteNonQuery();

I am getting the following error:

"Could not impersonate the client during assembly file operation."

The CLR function is invoked from Service Broker internal activation stored procedure.

"SELECT user_name()" returns dbo just before CREATE ASSEMBLY execution.

SqlContext.WindowsIdentity.Name is "NT AUTHORITY\SYSTEM" as the Data Engine runs with the LocalSystem account.

How do I create a the necessary security context for "CREATE ASSEMBLY" to succeed ?

Service Broker Queue activation with EXECUTE AS = "SELF", "OWNER", domain account or dbo, all result in the above error. The Service Broker assembly having the internal activation stored procedure is registered "unsafe".

Many Thanks.

Bulk purge queue?

$
0
0

Short of dropping the queue and recreating, is there a good way to mass purge all messages for a given queue?

Broker Error : type="subscribe" source="statement" info="invalid"

$
0
0

i ran these on the database

use DB

GRANT CREATE PROCEDURE TO USER

GRANT CREATE QUEUE TO USER

GRANT CREATE SERVICE TO USER

 

Use DB

GRANT SUBSCRIBE QUERY NOTIFICATIONS TO USER

Use DB

GRANT RECEIVE ON QueryNotificationErrorsQueue TO USER

ALSO ENABLE_BROKER

 

this is the query

select column(char(12) FROM   dbo.table

 

 

running a profile i get this error

type="subscribe" source="statement" info="invalid"

 

I've looked here

http://msdn.microsoft.com/en-us/library/ms189308.aspx

and here

http://msdn.microsoft.com/en-us/library/aewzkxxh(VS.80).aspx

 

can't figure this out....drive'n me crazy

it's just on this database...i can get it to work on other databases

Handling end dialog message in async trigger scenario

$
0
0

I have ServiceA which send message to Service B.  Service A lives in a sql trigger and doesn't wait for a response from Service B.  Service B pulls the message off the queue and closes the dialogue.  This generates an End Dialog message that was intended for Service A but Service A has long since moved on.  Is it ok for service B to subsequently process the End Dialog message in this scenario?

Service A context = Sql Trigger

Service B context = .NET windows service manually calling TSQL Receive

Sql Dependency onchange event not firing every time c#

$
0
0

i have implemented sql dependency in windows service. when data will be changed in table then onchange event will fire and from there i am invoking a web service.

i will share my full code. i tested many time in my pc before installing the window service production pc. i works. suppose if i install in production pc today then it works for today but when i test next day then i was onchange event not firing.

so i found onchange event firing only first day and from the next day onchange event not firing. may be i made some mistake in code. so it is my request please some one see my code in details and help me where i made the mistake for which it is not working properly.

public partial class PartIndexer : ServiceBase
{
    static string connectionString = "server=xxx;uid=xxx;password=xxx;database=xxx;Pooling=true;Connect Timeout=20;";
    SqlDependency dep;

    public PartIndexer()
    {
        InitializeComponent();
    }

    private string GetLoggedInUser()
    {
        string userName = "";
        if (System.Security.Principal.WindowsIdentity.GetCurrent() != null)
        {
            userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        }
        return userName;
    }

    #region OnStart
    protected override void OnStart(string[] args)
    {
        BBALogger.Write("PartIndexer Service OnStart called start", BBALogger.MsgType.Info);
        RegisterNotification();
        MailSend(); // notification mail send
        BBALogger.Write("PartIndexer Service OnStart called end, logged in user " + GetLoggedInUser(), BBALogger.MsgType.Info);
    }
    #endregion


    #region RegisterNotification
    /// <summary>
    /// RegisterNotification
    /// this is main routine which will monitor data change in ContentChangeLog table
    /// </summary>
    private void RegisterNotification()
    {
        string tmpdata = "";
        BBALogger.Write("PartIndexer Service RegisterNotification called start", BBALogger.MsgType.Info);

        System.Data.SqlClient.SqlDependency.Stop(connectionString);
        System.Data.SqlClient.SqlDependency.Start(connectionString);

        try
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT TestTable FROM [dbo].ContentChangeLog";
                dep = new SqlDependency(cmd);
                dep.OnChange += new OnChangeEventHandler(OnDataChange);
                SqlDataReader dr = cmd.ExecuteReader();
                {
                    if (dr.HasRows)
                    {
                        dr.Read();
                        tmpdata = dr[0].ToString();
                    }
                }
                dr.Dispose();
                cmd.Dispose();
            }
        }
        catch (Exception ex)
        {
            BBALogger.Write("PartIndexer Service RegisterNotification Error "+ex.Message.ToString(), BBALogger.MsgType.Error);
        }
        finally
        {
            BBALogger.Write("PartIndexer Service RegisterNotification called end", BBALogger.MsgType.Info);

        }

    }
    #endregion

    #region OnDataChange
    /// <summary>
    /// OnDataChange
    /// OnDataChange will fire when after data change found in ContentChangeLog table
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void OnDataChange(object sender, SqlNotificationEventArgs e)
    {
        ((SqlDependency)sender).OnChange -= OnDataChange;

        BBALogger.Write("PartIndexer Service RegisterNotification called end", BBALogger.MsgType.Info);

        if (e.Source == SqlNotificationSource.Timeout)
        {
    MailSend(); // notification mail send
    BBALogger.Write("PartIndexer Service SqlNotificationSource.Timeout error", BBALogger.MsgType.Error);

            Environment.Exit(1);
        }
        else if (e.Source != SqlNotificationSource.Data)
        {
        MailSend(); // notification mail send
            BBALogger.Write("PartIndexer Service SqlNotificationSource.Data", BBALogger.MsgType.Error);

            Environment.Exit(1);
        }
        else if (e.Type == SqlNotificationType.Change)
        {
            StartIndex();
            BBALogger.Write("PartIndexer Service Data changed", BBALogger.MsgType.Info);
        }
        else
        {
            BBALogger.Write(string.Format("Ignored change notification {0}/{1} ({2})", e.Type, e.Info, e.Source), BBALogger.MsgType.Warnings);
        }

        RegisterNotification();
    }
     #endregion

    #region StartIndex
    /// <summary>
    /// StartIndex
    /// this routine will call web service in bba reman website which will invoke routine to re-index data
    /// </summary>
    void StartIndex()
    {
        // calling web service if change is notified

    }
    #endregion

    #region MailSend
    /// <summary>
    /// MailNotify
    /// fire mail when apps start & exit
    /// </summary>
    /// <param name="strStatus"></param>
    void MailSend()
    {
    // mail send code
    }
    #endregion

    #region OnStop
    protected override void OnStop()
    {
        BBALogger.Write("PartIndexer Service StartIndex called end, logged in user " + GetLoggedInUser(), BBALogger.MsgType.Info);
        System.Data.SqlClient.SqlDependency.Stop(connectionString);
        MailNotify("STOPPED");
    }
    #endregion
}

another issue i noticed that when i start my windows service and leave it run in one day when i try to stop or restart the the windows service then i saw windows can not stop the service. it means definitely there is some flaw in my code which i am not being able to point out. so please help me.

one guy review my code and said i am creating queue each time that causes problem ?

is it true that my code will create a queue each time. if yes then creating queue each time causes what kind of problem ? need details. thanks


Critical: The query notification dialog on conversation handle Sqlserver 2008R2 SP2

$
0
0

Hi Experts,

Basically i am from oracle apps background but taking care of adhoc-queries of sqlserver. i have see the following errors in sqlserver logs. Can you please guide how to trouble shoot it.

Server Configuration
===============

Edition ProductLevel ServerType VersionNumber
Enterprise Edition (64-bit) SP2 CLUSTERED Microsoft SQL Server 2008 R2 (SP2) - 10.50.4000.0 (X64) Jun 28 2012 08:36:30 Copyright (c) Microsoft Corporation Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) 

Critical: The query notification dialog on conversation handle '{A4CC2645-63E9-E411-8DA3-00215A9B005E}.' closed due to the following error: '<?xml version="1.0"?><Error xmlns="http://schemas.microsoft.com/SQL/ServiceBroker/Error"><Code>-8490</Code><Description>Cannot find the remote service 'SqlQueryNotificationService-6cd8cf6a-c817-4fda-8ff4-92e4da550f92' because it does not exist.</Description></Error>'.

Checked couple of blogs and forums, came to know its due to service broker enabling if i am right how to solve this issue and what is the exact use of this Service Broker.

Appreciate your response, Thanks in advance.

--Sai

Service Broker Disabled but it comes back showing its Enabled.

$
0
0

Used to disable SERVICE BROKER in single user mode :

ALTER DATABASE [Database_name] SET DISABLE_BROKER;

SELECT is_broker_enabled FROM sys.databases WHERE name = 'Database_name';

when i checked the database after disabling, it shows 'is_broker_enabled'=1

wht can be problem.


Naveen| Press Yes if the post is useful.

Insert a record using sql broker service

$
0
0

Hi Guys,

I need some samples to use sql broker service for creation of a record in other database in order to reduce the delay in creating number of records at the same time.

Cannot find the remote service GUID it does not exist

$
0
0
Hello,

I have troublesome problem with service broker. I move a lot of databases
from one server to another servers (by backup and restore) but always have problem with
databases where service broker is enabled. Bellow is my scenario:

1. backup database db1 (srv1)
2. restore db1 (srv2, srv3, srv4.....)
3. drop manually Services and Queues, only green filed
(http://img122.imageshack.us/img122/549/clipqk5.jpg) (srv2, srv3, srv4.....)
4. alter database db1 set enable_broker (srv2, srv3, srv4.....)

After that SQL Server log on each server is full bellow errors, nobody so
far can help me Sad

Could you help, please

############
SRV2
03/03/2008 18:54:41,spid21s,Unknown,The query notification dialog on
conversation handle '{E6BF547B-0AE6-DC11-A4BD-00E08127B819}.' closed due to
the following error: '<?xml version="1.0"?><Error
xmlns="http://schemas.microsoft.com/SQL/ServiceBroker/Error"><Code>-8490</Code><Description>Cannot find the remote service
&apos;SqlQueryNotificationService-53c1dc9b-7d22-4310-a5b6-18ac97ae4bf3&apos;
because it does not exist.</Description></Error>'.
03/03/2008 18:54:41,spid27s,Unknown,The query notification dialog on
conversation handle '{AA68F7D8-F7E5-DC11-A4BD-00E08127B819}.' closed due to
the following error: '<?xml version="1.0"?><Error
xmlns="http://schemas.microsoft.com/SQL/ServiceBroker/Error"><Code>-8490</Code><Description>Cannot find the remote service

SERV3
03/02/2008 00:32:25,spid21s,Unknown,The query notification dialog on
conversation handle '{2BC70B27-F1E3-DC11-AB50-0017085CAA29}.' closed due to
the following error: '<?xml version="1.0"?><Error
xmlns="http://schemas.microsoft.com/SQL/ServiceBroker/Error"><Code>-8490</Code><Description>Cannot find the remote service
&apos;SqlQueryNotificationService-abc6a7ce-53f1-411e-85a7-50c09e2699e1&apos;
because it does not exist.</Description></Error>'.
03/02/2008 00:29:20,spid22s,Unknown,The query notification dialog on
conversation handle '{DF112861-F9E3-DC11-AB50-0017085CAA29}.' closed due to
the following error: '<?xml version="1.0"?><Error
xmlns="http://schemas.microsoft.com/SQL/ServiceBroker/Error"><Code>-8490</Code><Description>Cannot find the remote service

SRV4
03/02/2008 16:15:07,spid25s,Unknown,The query notification dialog on
conversation handle '{4179E578-7DE4-DC11-9A69-00145E1B71FC}.' closed due to
the following error: '<?xml version="1.0"?><Error
xmlns="http://schemas.microsoft.com/SQL/ServiceBroker/Error"><Code>-8490</Code><Description>Cannot find the remote service
&apos;SqlQueryNotificationService-01e16746-e114-4e8c-b587-91e93768a9a8&apos;
because it does not exist.</Description></Error>'.
03/02/2008 16:15:07,spid20s,Unknown,The query notification dialog on
conversation handle '{5079E578-7DE4-DC11-9A69-00145E1B71FC}.' closed due to
the following error: '<?xml version="1.0"?><Error
xmlns="http://schemas.microsoft.com/SQL/ServiceBroker/Error"><Code>-8490</Code><Description>Cannot find the remote service
############




SqlDependency class throws "The given key was not present in the dictionary." during SqlDependency.Start()

$
0
0

Today I ran into an issue when using the .Net SqlDependency class when calling SqlDependency.Start(connectionName, queueName). The class would throw the exception "KeyNotFound" - "The given key was not present in the dictionary.". After scratching my head for a few moments I found that in the queue there were a number of messages stuck in the queue that did not have conversation handles that did not exist.

It is possible that somewhere in my code it did not call SqlDependency.Stop() when the webserver was recycled or due to an unhandled exception - but could easily have been Service Broker itself.

I found the solution was to clear the queue of any items that had invalid conversation handles (or completely clear the queue).

DECLARE item_cursor CURSOR LOCAL FAST_FORWARD FOR
    SELECT s.conversation_handle FROM clients.dbo.MySBQueueName s LEFT JOIN sys.conversation_endpoints e ON e.conversation_handle=s.conversation_handle WHERE e.conversation_handle IS NULL;
	-- or completely clear it below
	--SELECT s.conversation_handle FROM clients.dbo.MySBQueueName s;
OPEN item_cursor
DECLARE @conversation UNIQUEIDENTIFIER
FETCH NEXT FROM item_cursor INTO @conversation
WHILE @@FETCH_STATUS = 0
BEGIN
    END CONVERSATION @conversation WITH CLEANUP
    FETCH NEXT FROM item_cursor INTO @conversation
END

There wasn't much on Google about this error (though people are reporting it), I hope this helps someone.


Michael Brown, 360 Replays Ltd. (don't forget that 'Mark As Answer' button!)


Viewing all 461 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>