Showing posts with label behaviours. Show all posts
Showing posts with label behaviours. Show all posts

Wednesday, March 28, 2012

Received message constantly processed.

Hello,

when is seemd that everything works some weird behaviours comes out.

I try to summarize the problem without to post the complete code.

Service Broker is set to have a dialog between two databases on the same SQL Server instance.

The Initiator queue has retention=on and there is an activation SP to handle errors and Target's end dialog message.

The Target queue has retention=off, MAX_READER =1 and there is an activation SP to receive the message (WAIT FOR (RECEIVE (1) ...), TIMEOUT 30000 and do something with this message (sample insert into a DB).

The conversation has a Timeout Dialog to end the dialog after a while.

The problem that the message is constantly processed. The Process doens't stop is I end the dialof after the processing either.

n.b.the Receive is within a Transation that I commit at the end.

some other informations that in the meanwhile I found out :

This was my complete WAIT FOR(RECEIVE :

WAITFOR ( RECEIVE top(1) -- just handle one message at a time
@.message_type=message_type_id, --the type of message received
@.messagetypename=message_type_name,
@.message_body=message_body, -- the message contents
@.dialog = conversation_handle -- the identifier of the dialog this message was received on
FROM [TargetQueue]
), timeout 1000;

if (@.@.ROWCOUNT = 0)
BEGIN
COMMIT;
BREAK;
END

IF I delet TIMEOUT 1000, everything works as expected ...
Inside if (@.@.ROWCOUNT = 0)BEGIN..END I wrote also an Insert into a table to see wheter the end of the queue was reached but this insert never occurs (neither with not without timeout)
I'm happy that it works what if this is the solution, it make no sense to me!

Any ideas?
Thank you!

M.B.

Thank you very much

M.B.

This sounds like there are messages constantly comming and causing the WAITOFR(RECEIVE...) to get another message while is waiting. Is this the case?|||

Hi,

it shouldn't be!!

I suppose that the only way to understand what is going on is to use the SQL Profiler.

Isn't?

Thank you

|||

Hi,

I tried to use the SQL server Profiler to understand what is going on when I send/receive the message.

First at all on both queues(initiator and target) I have RETENTION = OFF so the check if @.@.ROWCOUNT = 0 will be never true because the message stays in the queue till the END of the CONVERSATION and in my case the conversation will end when there is an error or when the conversation timeout expires; otherwise I send/receive using the same conversation handle.

If I write:

WAITFOR (

RECEIVE top(1) ...

), TIMEOUT 5000;

In the SQL Server Profiler I seen that the SP that is called after the receive statement and that processes the message is constantly executed.

If I write:

WAITFOR (

RECEIVE top(1) ...

)

In the SQL Server Profiler I seen that the SP that is called after the receive statement and that processes the message is executed only ONCE.

I'm quite confuse ... I thought that is better to use a timeout but if I use I have that strange behaviour!!

Any helps/advices?
Thankx

Marina B.

|||

@.@.ROWCOUNT is set by RECEIVE to the number of rows (messages) actually returned, so it doesn't matter if RETENTION is ON or OFF.

From your description of the problem is clear that your procedured does not correctly detect when RECEIVE returned no rows, so it continues to spin on empty RECEIVEs that timeout. Can you post the actual code of the procedure?

|||

Hi Remus,

thank you very much for your answer...

Maybe it is not necessarely but this is the SP wuch start the conversation (activated by a trigger on a insert )

PROCEDURE [dbo].[sp_sendInserted]

@.MessageXML nvarchar(max)

AS

BEGIN

DECLARE @.dialog_handle uniqueidentifier;

DECLARE @.dialog_id uniqueidentifier;

DECLARE @.msg XML;

DECLARE @.Error INT;

SET NOCOUNT OFF;

BEGIN TRY

BEGIN TRANSACTION;

set @.msg = Convert(xml,@.MessageXML);

WHILE (1=1)

BEGIN

set @.dialog_handle = (select conversation_handle from sys.conversation_endpoints where far_service='ReceivedService');

if @.dialog_handle is null

--Begin new dialog

BEGIN

BEGIN DIALOG CONVERSATION @.dialog_handle

FROM SERVICE [SendService]

TO SERVICE 'ReceivedService'

ON CONTRACT [MainContract]

WITH ENCRYPTION = OFF;

BEGIN CONVERSATION TIMER (@.dialog_handle) TIMEOUT = 600;

END;

SEND ON CONVERSATION @.dialog_handle

MESSAGE TYPE MyMessage (@.msg); END

COMMIT TRANSACTION;

END TRY

BEGIN CATCH

IF XACT_STATE() = -1

BEGIN

ROLLBACK TRANSACTION;

END

Insert into [dbo].[tblErrorXMLMessages] values(@.MessageXML,@.dialog_handle,@.dialog_id,null,null,ERROR_NUMBER(),ERROR_MESSAGE(),ERROR_PROCEDURE(),getdate());

END CATCH

END

And this is the SP activated on the ReceiveQueue:

PROCEDURE [dbo].[OnReceivedMessage]

AS

DECLARE @.message_type INT;

DECLARE @.messagetypename NVARCHAR(1000);

DECLARE @.XMLmessage_body XML;

DECLARE @.dialog UNIQUEIDENTIFIER;

DECLARE @.dialog_id UNIQUEIDENTIFIER;

DECLARE @.ErrorSave INT;

DECLARE @.ErrorDesc NVARCHAR(100);

SET NOCOUNT ON;

WHILE (1 = 1)

BEGIN

BEGIN TRY

BEGIN TRANSACTION;

WAITFOR (

RECEIVE top(1) -- just handle one message at a time

@.message_type=message_type_id, --the type of message received

@.messagetypename=message_type_name,

@.XMLmessage_body=message_body, -- the message contents

@.dialog = conversation_handle -- the identifier of the dialog this message was received on

FROM [ReceivedQueue]

)--, timeout 5000; -- if the queue is empty for three second

--Before to close the conversation I get the Conversation ID to be stored in the tblErrorXMLMessages

set @.dialog_id = (select conversation_id from sys.Conversation_endpoints where conversation_handle = @.dialog);

-- If we didn't get anything, bail out

if (@.@.ROWCOUNT = 0)

BEGIN

INSERT INTO [TestReceiver].[dbo].[tblReceived] VALUES('Received SP','<ReceivedQueue>NO more messages in the queue</ReceivedQueue>');

COMMIT;

BREAK;

END

If (@.messagetypeName = N'MyMessage')

BEGIN

DECLARE @.ret integer;

EXECUTE @.ret = [TestReceiver].[dbo].[sp_ShredXMLMessageToRelationalData] @.XMLmessage_body; --SP that PROCESSES THE MESSAGE RECEIVED ON THE QUEUE

if (@.ret <> 0)

INSERT INTO [TestSender].[dbo].[tblErrorXMLMessages] VALUES

(cast(@.XMLmessage_body as nvarchar(max)),@.dialog,@.dialog_id,null,null,0,null,null,getdate());

END

ELSE IF (@.messagetypeName = N'EndOfStream')

BEGIN

END CONVERSATION @.dialog;

END

-- Check for the Error Dialog message.

ELSE IF(@.messagetypeName=N'http://schemas.microsoft.com/SQL/ServiceBroker/Error')

BEGIN

DECLARE @.WrongXMLMessage NVARCHAR(max);

set @.WrongXMLMessage = (Select message_body FROM dbo.TradeReceivedQueue WITH (NOLOCK) where Message_type_name = 'MyMessage' and status = 3 and conversation_handle=@.dialog);

DECLARE @.Error int;

DECLARE @.ErrorDescription nvarchar(4000);

WITH XMLNAMESPACES

('http://schemas.microsoft.com/SQL/ServiceBroker/Error' as ssb)

SELECT @.Error = cast(@.XMLMessage_Body as XML).value('(//ssb:Error/ssb:Code)[1]','INT'),

@.ErrorDescription = cast(@.XMLMessage_Body as XML).value('(//ssb:Error/ssbBig Smileescription)[1]','nvarchar(4000)')

IF @.WrongXMLMessage is null

BEGIN

SET @.WrongXMLMessage = N'<Error>Error retrieving the wrong XML message from the sender queue</Error>';

END

Insert into [TestSender].[dbo].[tblErrorXMLMessages] values(@.WrongXMLMessage,@.dialog,@.dialog_id,'MArina','ReceivedQueue',@.Error,@.ErrorDescription,ERROR_PROCEDURE(),getdate());

--After the End Conversation all messages from the queue are deleted

END CONVERSATION @.dialog;

END

COMMIT TRANSACTION;

END TRY

BEGIN CATCH

insert into [testReceiver].[dbo].[tblReceived] values('Error','<Marina>Error in SP Traget QUEUE</Marina>');

END CATCH

END

Let me know whether you need more info regarding contract, queues etc..etc..

Thank you very much!

Marina B.

|||

marina B. wrote:

WAITFOR (

RECEIVE top(1) -- just handle one message at a time

@.message_type=message_type_id, --the type of message received

@.messagetypename=message_type_name,

@.XMLmessage_body=message_body, -- the message contents

@.dialog = conversation_handle -- the identifier of the dialog this message was received on

FROM [ReceivedQueue]

)--, timeout 5000; -- if the queue is empty for three second

--Before to close the conversation I get the Conversation ID to be stored in the tblErrorXMLMessages

set @.dialog_id = (select conversation_id from sys.Conversation_endpoints where conversation_handle = @.dialog);

-- If we didn't get anything, bail out

if (@.@.ROWCOUNT = 0)

BEGIN

INSERT INTO [TestReceiver].[dbo].[tblReceived] VALUES('Received SP','<ReceivedQueue>NO more messages in the queue</ReceivedQueue>');

COMMIT;

BREAK;

END

SQL Server 2005 Books Online wrote:

@.@.ROWCOUNT (Transact-SQL)

Returns the number of rows affected by the last statement

RECEIVE is not the last statement before you check @.@.ROWCOUNT.

|||

As always,

my stupid mistake.

I thought that I was making a mess with the transaction!!

I moved the @.@.Rowcount check after the RECEIVE statement and it worked..

I should read more CAREFULLY the BOL !!

Thankx!!

Marina B.

Wednesday, March 21, 2012

Rebuild master db

I've recently had to rebuild the master database using the rebuild -m
utility. However, i've noticed some strange behaviours in my application
since in particular that a few errors are mssing from the sysmessages table.
What would be the preferred plan of action from here to get the system
tables back to a similar state before the rebuild ( I don't have a usable
backup of the master database i'm afraid)? Would it be to reapply the latest
SP?
I'm using SQL2k on a win2k box and prior to the rebuild of master, it was
patched up to SP3.
Thanks
RichYes, it is probable that a service pack adds rows to sysmessages. Do you
know the error number of the missing rows?
Another alternative is that you, or your application added those rows.
Error number < 50000 should mean they were added by SQL Server (service pack
is likely), > 50000 mean application.
--
Tibor Karaszi, SQL Server MVP
Archive at:
http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
"Rich" <richbrownesq@.hotmail.com> wrote in message
news:%23UwsY15pDHA.744@.tk2msftngp13.phx.gbl...
> I've recently had to rebuild the master database using the rebuild -m
> utility. However, i've noticed some strange behaviours in my application
> since in particular that a few errors are mssing from the sysmessages
table.
> What would be the preferred plan of action from here to get the system
> tables back to a similar state before the rebuild ( I don't have a usable
> backup of the master database i'm afraid)? Would it be to reapply the
latest
> SP?
> I'm using SQL2k on a win2k box and prior to the rebuild of master, it was
> patched up to SP3.
> Thanks
> Rich
>|||There's 25 missing errors, ids ranging from 1960 -> 21520 and I think most
of these are sql server errors.
Would you recommend reapplying the SP or could i just insert the missing
rows? The issue i have with this is that there could be other modifications
to the system databases that i'm unaware of and wouldn't be fixed without
the latest SP.
However, when i run select serverproperty('productlevel'), it tells me SP3
is installed. Do you know if this will prevent me installing it again?
"Tibor Karaszi" <tibor.please_reply_to_public_forum.karaszi@.cornerstone.se>
wrote in message news:OeoPM75pDHA.4004@.TK2MSFTNGP11.phx.gbl...
> Yes, it is probable that a service pack adds rows to sysmessages. Do you
> know the error number of the missing rows?
> Another alternative is that you, or your application added those rows.
> Error number < 50000 should mean they were added by SQL Server (service
pack
> is likely), > 50000 mean application.
> --
> Tibor Karaszi, SQL Server MVP
> Archive at:
>
http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
>
> "Rich" <richbrownesq@.hotmail.com> wrote in message
> news:%23UwsY15pDHA.744@.tk2msftngp13.phx.gbl...
> > I've recently had to rebuild the master database using the rebuild -m
> > utility. However, i've noticed some strange behaviours in my application
> > since in particular that a few errors are mssing from the sysmessages
> table.
> > What would be the preferred plan of action from here to get the system
> > tables back to a similar state before the rebuild ( I don't have a
usable
> > backup of the master database i'm afraid)? Would it be to reapply the
> latest
> > SP?
> >
> > I'm using SQL2k on a win2k box and prior to the rebuild of master, it
was
> > patched up to SP3.
> >
> > Thanks
> > Rich
> >
> >
>|||Yes, you should reapply the service pack. You are correct in other things
can be affected by the service pack, like bug fixes in system stored
procedures etc. The reason why you get sp3 is probably because the function
checks the .exe file and that didn't change because of your rebuild. I did a
search in the archives and came across below, amongst others (strange that I
did not find anything in BOL or KB that you need to reapply service pack).
http://tinyurl.com/ue4s
Or full URL:
http://groups.google.com/groups?q=rebuildm+%22service+pack%22+group:microsoft.public.sqlserver.*&hl=en&lr=&ie=UTF-8&group=microsoft.public.sqlserver.*&selm=4oheDoLQDHA.1724%40cpmsftngxa09.phx.gbl&rnum=5
--
Tibor Karaszi, SQL Server MVP
Archive at:
http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
"Rich" <richbrownesq@.hotmail.com> wrote in message
news:%234QgaA6pDHA.1672@.TK2MSFTNGP09.phx.gbl...
> There's 25 missing errors, ids ranging from 1960 -> 21520 and I think most
> of these are sql server errors.
> Would you recommend reapplying the SP or could i just insert the missing
> rows? The issue i have with this is that there could be other
modifications
> to the system databases that i'm unaware of and wouldn't be fixed without
> the latest SP.
> However, when i run select serverproperty('productlevel'), it tells me SP3
> is installed. Do you know if this will prevent me installing it again?
>
> "Tibor Karaszi"
<tibor.please_reply_to_public_forum.karaszi@.cornerstone.se>
> wrote in message news:OeoPM75pDHA.4004@.TK2MSFTNGP11.phx.gbl...
> > Yes, it is probable that a service pack adds rows to sysmessages. Do you
> > know the error number of the missing rows?
> >
> > Another alternative is that you, or your application added those rows.
> >
> > Error number < 50000 should mean they were added by SQL Server (service
> pack
> > is likely), > 50000 mean application.
> > --
> > Tibor Karaszi, SQL Server MVP
> > Archive at:
> >
>
http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
> >
> >
> > "Rich" <richbrownesq@.hotmail.com> wrote in message
> > news:%23UwsY15pDHA.744@.tk2msftngp13.phx.gbl...
> > > I've recently had to rebuild the master database using the rebuild -m
> > > utility. However, i've noticed some strange behaviours in my
application
> > > since in particular that a few errors are mssing from the sysmessages
> > table.
> > > What would be the preferred plan of action from here to get the system
> > > tables back to a similar state before the rebuild ( I don't have a
> usable
> > > backup of the master database i'm afraid)? Would it be to reapply the
> > latest
> > > SP?
> > >
> > > I'm using SQL2k on a win2k box and prior to the rebuild of master, it
> was
> > > patched up to SP3.
> > >
> > > Thanks
> > > Rich
> > >
> > >
> >
> >
>sql