Solved

Lock data trickle feed while admin in progress.

Posted on 2009-07-09
4
466 Views
Last Modified: 2013-11-05
Hi All,

I have two applications accessing a private database.

Feeder - Acts on events arriving from elsewhere and makes changes/additions to the data. Somewhere between once per day and hundreds per minute.

Admin - Presents reports on data and allows acknowledgement of states detected by Feeder. Acknowledgement results in deletion of records.

Both applications are in-house. Feeder will be running on or close to the server. Admin could be run anywhere on the estate but there will only be one copy of either component.

I need some way of locking out the feeder from making updates to the database while Admin is running.

I have seen in other comments:

BEGIN TRANSACTION
SELECT * FROM ATable (holdlock, tablockx) where 1 = 0
-- Do stuff for a while.
COMMIT TRANSACTION

If I run this from Admin am I right that Feeder will be locked out from any access to the table, even reading?

Why the 1 = 0? Is it to ensure we don't get any records back?

What if Admin gets switched off while the lock is in place? Can the lock be torn down?

How long will the lock hold? Admin may take an hour or more to process the results before committing its changes.

Would it be better to use a special record in a special table and hold a lock on that as a semaphore? If so how?

Thanks in advance. :)

Paul
0
Comment
Question by:PaulCaswell
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
kevin_u earned 250 total points
ID: 24819566
1 = 0 is just as you say.

Feeder will be locked out, but after a deadlock timeout you may get an error in feeder, but you can retry the transaction.  Feeder should also do a begin transaction and commit transaction if more than one insert/update/delete is being done.

If the connection is broken from admin, the lock is automatically released.

The lock will be held until Commit Transaction or Admin terminates or disconnects from ther server.

Special records are generally a pain .

The proper way to deal with this is to learn about transactions and transaction isolation levels.  It allows one process to aquire an intact snapshot with little or no interference from the other processes.  It involves using Begin transaction and Commit Transaction around your update processes.  Transaction Isolation Levels determine whether Admin can receive records that are "dirty" (no commited) or not, therefore give you  the consistancy of data across the tables that are updated.

http://msdn.microsoft.com/en-us/library/ms173763.aspx
0
 
LVL 16

Author Comment

by:PaulCaswell
ID: 24821201
Thanks Kevin,

This does seem to lock Feeder out nicely. Will it fail eventually or just sit there forever waiting for access?

Is there a way for Feeder to detect whether the lock is on or not without accessing the locked table?

Paul
0
 
LVL 12

Expert Comment

by:kevin_u
ID: 24824216
As I said in my first comment:

Feeder will be locked out, but after a deadlock timeout you may get an error in feeder, but you can retry the transaction.  Feeder should also do a begin transaction and commit transaction if more than one insert/update/delete is being done.

Detecting the lock is not something that is normally available.   Transactions or queries should normally happen quickly.  Again transaction isolation is the way to deal with this.
0
 
LVL 16

Author Comment

by:PaulCaswell
ID: 24824568
Thanks Kevin,

I do want Feeder to be locked out and this does do the job. I left it waiting for over an hour and it was still waiting. :) There wont be a deadlock as only one table is locked.

I just felt that this length of time was a little long for it to be incommunicado. I'd like to try to find a way to drop the timeout to a few minutes or check for a lock before going in. I'll do some further research

The feeder does all of its work in one transaction for exactly this reason, I want it to be locked out of the table.

Thanks for your help.

Paul
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now