Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server and message queues

Tags:

c#

sql

sql-server

I'm trying to build a reliable message service, or at least that's how I would describe it.

Here's my problem: I have a table, I insert data into this table, I have at least two applications which select data from this table. However, I need a reliable way for the two different applications to never select the same rows at any given time.

How would I go about writing a transaction or select statement that is guaranteed to not select the same rows that other applications have recently selected.

I'm not an SQL Server expert, but I would expect something similar to this.

Select work from table, this will give the application exclusive access to some rows. The application would then process those rows. Some rows get deleted, some rows are returned to the database.

My main concern is that if the application fails to complete it's processing, SQL Server would eventually time out and return the checked out data to the application data pool.

like image 519
John Leidegren Avatar asked Apr 25 '09 08:04

John Leidegren


People also ask

What is message queue in SQL Server?

The Message Queue task allows you to use Message Queuing (also known as MSMQ) to send and receive messages between SQL Server Integration Services packages, or to send messages to an application queue that is processed by a custom application.

What is MQ in SQL?

The SQL MQ connection provider is a IBM Robotic Process Automation implementation that aims to facilitate and expand the options for working with queue providers.

What is SQL Server Servicebroker?

Service Broker provides queuing and reliable messaging for SQL Server. Service Broker is used both for applications that use a single SQL Server instance and applications that distribute work across multiple instances. Within a single SQL Server instance, Service Broker provides a robust asynchronous programming model.

When should message queue be used?

Queues make your data persistent, and reduce the errors that happen when different parts of your system go offline. By separating different components with message queues, you create more fault tolerance. If one part of the system is ever unreachable, the other can still continue to interact with the queue.


3 Answers

this is quite an old question and probably the author's problem is long gone now but there's a little SQL trick that solves it and can be useful to others: the 'readpast' hint. With readpast sql select will skip locked rows in a query. What you want is to grab first unlocked row and immediately lock it so no one else will get it:

select top 1 * 
from messages with(readpast, updlock) 
order by id

This will select and lock the first free message in the table and so no one else will be able to modify it. Actually I'm using the same approach in SQL-based message bus: http://http://code.google.com/p/nginn-messagebus/ and it works very smoothly (sometimes faster than MSMQ)

like image 41
nightwatch Avatar answered Sep 20 '22 13:09

nightwatch


Take a look at SQL Server Service Broker, it is designed for this kind of problem.

If you need a queue, use a queue.

like image 35
Richard Avatar answered Sep 19 '22 13:09

Richard


Add a 'Being Processed' Flag to each record, which is set in an 'atomic' way.

There have been several questions and many answers here on SO, quite similiar to this:

How do I lock certain SQL rows while running a process on them?

Queues against Tables in messaging systems

What’s the best way of implementing a messaging queue table in mysql

Posting Message to MSMQ from SQL Server

like image 166
Mitch Wheat Avatar answered Sep 16 '22 13:09

Mitch Wheat