Solved

Lock data trickle feed while admin in progress.

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

705 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