Link to home
Start Free TrialLog in
Avatar of PaulCaswell
PaulCaswellFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Lock data trickle feed while admin in progress.

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
ASKER CERTIFIED SOLUTION
Avatar of kevin_u
kevin_u
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PaulCaswell

ASKER

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
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.
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