Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Lock data trickle feed while admin in progress.

Posted on 2009-07-09
4
Medium Priority
?
476 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
kevin_u earned 1000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

610 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