home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.databases.sybase
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!paladin.american.edu!howland.reston.ans.net!usc!sol.ctr.columbia.edu!destroyer!cs.ubc.ca!newsserver.sfu.ca!sfu.ca!wolfgang
- From: wolfgang@fraser.sfu.ca (Wolfgang Richter)
- Subject: Re: Explicitly lock table
- Message-ID: <wolfgang.726345772@sfu.ca>
- Sender: news@sfu.ca
- Organization: Simon Fraser University, Burnaby, B.C., Canada
- References: <35833.salter@magnus.acs.ohio-state.edu>
- Date: Wed, 6 Jan 1993 18:42:52 GMT
- Lines: 56
-
- "John Salter" <salter@magnus.acs.ohio-state.edu> writes:
-
-
- >Is is possible to explicitly lock a table in Sybase to prevent
- >other users from inserting into table for a very short time? If
- >so, how is it done? Thanks.
-
- Here is a repost of information that I gleaned from this newsgroup
- some time ago.
-
- So far, the best solution that I have come across was supplied by Scott
- Elliott of AT&T Network Systems. I have implemented his method and it works
- fine. Here is what needs to be done:
-
- declare @id
- begin transaction
- if not exists (select * from table HOLDLOCK where id=@id)
- begin
- insert into table values (@id,...)
- .
- .
- .
- end
- commit transaction
-
- If two users execute this at the same time, the following will happen:
-
- User 1 and User 2 acquire shared locks on the table. They will each
- hold these locks until the end of the transaction (thanks to the HOLDLOCK
- keyword). User 1 will request an exclusive lock on the table in order
- to insert the new row. He will be blocked because User 2 still holds a
- shared lock! User 2 will now request an exclusive lock to insert a row
- also. He is also blocked since User 1 still holds his shared lock. We
- now have DEADLOCK!!!! Sybase will then choose a victim and abort one
- of the transactions. End result: ONLY 1 USER HAS ADDED THE ROW. You
- should also include some code to check the return value of the transaction
- to see if it was rolled back due to deadlock. If so, repeat the request.
-
-
- Good Luck...
- --
-
-
- +-------------------------------------------------------------------+
- | Stuart A. Stakoff | sstakoff@csfb1.fir.fbc.com |
- | First Boston Corporation | |
- | (212)909-4926 | (212)688-0913 - FAX |
- +------------------------------------------------------------------+
-
- --
- -- Wolfgang Richter (e-mail: wolfgang@sfu.ca)
- -- Academic Computing Services
- -- Simon Fraser University
- -- Burnaby, B.C.
- -- Canada V5A 1S6
- -- Phone: 604-291-4449
-