Solved

Lock data trickle feed while admin in progress.

Posted on 2009-07-09
4
470 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 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Trigger or Function that updates table with old values 5 55
Stored Procedure needs owner to execute 5 48
Need to find substring in SQL 5 52
Database Mail Profiles 1 44
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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