all groups > sql server programming > may 2007 >
You're in the

sql server programming

group:

Lock all tables in a transaction


Lock all tables in a transaction Marty
5/22/2007 10:38:00 PM
sql server programming:
I am writing a holiday booking database. I have an absence table and a rules
table.
For any one day only X days holiday can be booked, where X is stored in the
rules table.
So when booking holiday we lookup X in the rules table, then get the count
of records in the absence table for the date we want to book. If the count is
less than X, we book the time.
I want to prevent 2 people from reading X (say X=100), when there are 99
records for today already and both entering a new record.
Each step in the process is done in a separate Stored procedure ( looking up
x, getting the count etc) and these are called from a main Stored procedure,
which has a start transaction at the top and end transaction at the bottom.
Once this transaction begins I need to lock both tables, at a minimum making
the rules unreadable and the absence table unwritable.
I currently do this:
Begin transaction T1
select * from rules with (TABLOCKX)
select * from absence with (TABLOCKX)

which seems to do the trick but I wonder if this is overkill.

Would I get away with this and would it be better ?

Begin transaction T1
select * from rules with (TABLOCKX)

I assume that if a second person fires off the same Stored procedure, the
first thing it will do is try to select * from rules and have to wait for the
first person's Stored procedure to finish completley before continuing. Is
this correct ?
--
Re: Lock all tables in a transaction Tony Rogerson
5/23/2007 12:00:00 AM
Hi Marty,

You do need a transaction - absolutely.

Using a tablockx will indeed prevent any read access against the table -
that really want you want?

Have a look at sp_getapplock, an example is here on my blog:
http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/06/30/855.aspx

--
Tony Rogerson, SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson
[Ramblings from the field from a SQL consultant]
http://sqlserverfaq.com
[UK SQL User Community]


[quoted text, click to view]
Re: Lock all tables in a transaction TheSQLGuru
5/23/2007 1:07:03 PM
1) You will need an overarching transaction

2) consider using WITH (ROWLOCK, UPDLOCK) on the SELECTs you use to grab the
values. This "should" lock just those values and only for the duration of
the total transaction. Other requests to get the X value will be blocked
and queue up, finishing once the original 99 grabber is committed or rolled
back. If the initial 99 grabber updates the value to 100, the next one in
he queue will see 100 and disallow the activity.

--
TheSQLGuru
President
Indicium Resources, Inc.

[quoted text, click to view]

RE: Lock all tables in a transaction weilu NO[at]SPAM online.microsoft.com
5/28/2007 12:00:00 AM
Hi ,

How is everything going? Please feel free to let me know if you need any
assistance.

Sincerely,

Wei Lu
Microsoft Online Community Support

==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
AddThis Social Bookmark Button