473,508 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tansaction/locking MSSQL2000

(MSSQL2000) I have read the transaction/locking sections in the
MS-help, online and several books. What I want to understand is the
transaction behavior in single statements [not a BEGIN TRANSACTION
Statement1, Statement2... COMMIT].

If I have a Table: "Letters" with 1 column "L" and the table presently
has rows{A,B,C,D}

Case 1 (Insert):
First start transaction T1 "SELECT * FROM Letters"
Next start transaction T2 [separate connection] "INSERT INTO Letters
VALUES( 'Z' )"
Is it possible that T2 ends before T1 and the select returns
{A,B,C,D,Z}
Is it possible that T1 ends before T2 and the select returns
{A,B,C,D} [No 'Z']
Is this a race condition and I need to use a TABLOCK or TABLOCKX;
and are TABLOCK/TABLOCKX only hints? I mean does the use of TABLOCK
guarantee a lock on the table? Do I need to use 'SET TRANSACTION
ISOLATION LEVEL SERIALIZABLE' and if I use 'TRANSACTION ISOLATION
LEVEL' is there a means of telling the system which tables I will
touch so that I can avoid a deadlock [upfront tell the system what
tables I need to lock so there is not a race later]?

Case 2 (Delete basically the same):
First start transaction T1 "SELECT * FROM Letters"
Next start transaction T2 "DELETE FROM Letters L = 'D'"
Is it possible that T2 ends before T1 and the select returns {A,B,C}
[No 'D']
Is it possible that T1 ends before T2 and the select returns
{A,B,C,D}

Case 3 (Update basically the same):
First start transaction T1 "SELECT * FROM Letters"
Next start transaction T2 "UPDATE Letters SET L = 'Z'"
Is it possible that T2 ends before T1 and the select returns
{A,B,Z,Z} [Some letters were seen to become 'Z']
Is it possible that T1 ends before T2 and the select returns
{A,B,C,D}
Jul 20 '05 #1
3 2599
On 23 Aug 2004 22:52:34 -0700, heynothanks wrote:
(MSSQL2000) I have read the transaction/locking sections in the
MS-help, online and several books. What I want to understand is the
transaction behavior in single statements [not a BEGIN TRANSACTION
Statement1, Statement2... COMMIT].

If I have a Table: "Letters" with 1 column "L" and the table presently
has rows{A,B,C,D}

Case 1 (Insert):
First start transaction T1 "SELECT * FROM Letters"
Next start transaction T2 [separate connection] "INSERT INTO Letters
VALUES( 'Z' )"
Is it possible that T2 ends before T1 and the select returns
{A,B,C,D,Z}
Is it possible that T1 ends before T2 and the select returns
{A,B,C,D} [No 'Z']
Is this a race condition and I need to use a TABLOCK or TABLOCKX;
and are TABLOCK/TABLOCKX only hints? I mean does the use of TABLOCK
guarantee a lock on the table? Do I need to use 'SET TRANSACTION
ISOLATION LEVEL SERIALIZABLE' and if I use 'TRANSACTION ISOLATION
LEVEL' is there a means of telling the system which tables I will
touch so that I can avoid a deadlock [upfront tell the system what
tables I need to lock so there is not a race later]?

Case 2 (Delete basically the same):
First start transaction T1 "SELECT * FROM Letters"
Next start transaction T2 "DELETE FROM Letters L = 'D'"
Is it possible that T2 ends before T1 and the select returns {A,B,C}
[No 'D']
Is it possible that T1 ends before T2 and the select returns
{A,B,C,D}

Case 3 (Update basically the same):
First start transaction T1 "SELECT * FROM Letters"
Next start transaction T2 "UPDATE Letters SET L = 'Z'"
Is it possible that T2 ends before T1 and the select returns
{A,B,Z,Z} [Some letters were seen to become 'Z']
Is it possible that T1 ends before T2 and the select returns
{A,B,C,D}


If the connection is in autocommit transaction mode (the default if no SET
IMPLICIT_TRANSACTIONS ON has been set), then every statement gets its own
transaction. It's as if every statement started with BEGIN TRANSACTION And
ended with END TRANSACTION.

If the connection is in implicit transaction mode, then the first statement
(SELECT, UPDATE, DELETE, INSERT, or some other types - see books online)
starts a transaction WHICH WILL REMAIN OPEN UNTIL you explicitly use COMMIT
TRANSACTION or else disconnect the connection. This can definitely be a
gotcha if you leave connections open for a long time!

In either mode, for the scenarios you describe, there is no way that
statement T2 can interfere with statement T1. If you were in implicit
transaction mode, then statement T1 would, I believe, start a read lock on
table Letters, which would not be released until the next COMMIT
TRANSACTION or until you closed the connection, and statement T2 would hang
until that happened. If you were in autocommit transaction mode, then
statement T1 would block T2 until T1 completed.

Only if you specifically used the NOLOCK hint on T1, would it be possible
for T2 to screw things up. I believe the behavior would then depend on the
specific timing.

I am not an expert on this; someone else in the group may have better
information.
Jul 20 '05 #2
heynothanks (he*********@yahoo.com) writes:
(MSSQL2000) I have read the transaction/locking sections in the
MS-help, online and several books. What I want to understand is the
transaction behavior in single statements [not a BEGIN TRANSACTION
Statement1, Statement2... COMMIT].

If I have a Table: "Letters" with 1 column "L" and the table presently
has rows{A,B,C,D}

Case 1 (Insert):
First start transaction T1 "SELECT * FROM Letters"
Next start transaction T2 [separate connection] "INSERT INTO Letters
VALUES( 'Z' )"
Is it possible that T2 ends before T1 and the select returns
{A,B,C,D,Z}
Is it possible that T1 ends before T2 and the select returns
{A,B,C,D} [No 'Z']
Yes, this is possible with the default transaciotn isolation level
which is READ COMMITTED, which says that once you have read a row,
you release that lock.

Here is a set-up to play with this kind of situation:

CREATE FUNCTION delay() RETURNS INT AS
BEGIN
EXEC master.dbo.xp_cmdshell 'osql -E -Q "WAITFOR DELAY ''00:00:05''"'
RETURN 0
END
go
CREATE TABLE a (a char(1) NOT NULL)
go
INSERT a (a)
SELECT 'A' UNION SELECT 'B' UNION
SELECT 'C' UNION SELECT 'D'
go
-- SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
SELECT *, dbo.delay() FROM a

Then run INSERT commands on a second connection.

For real-life queries, the existence of keys can affect the situation,
and you need to test each crucial case.

Is this a race condition and I need to use a TABLOCK or TABLOCKX;
and are TABLOCK/TABLOCKX only hints? I mean does the use of TABLOCK
guarantee a lock on the table? Do I need to use 'SET TRANSACTION
ISOLATION LEVEL SERIALIZABLE' and if I use 'TRANSACTION ISOLATION
LEVEL' is there a means of telling the system which tables I will
touch so that I can avoid a deadlock [upfront tell the system what
tables I need to lock so there is not a race later]?


You can specify (SERIALIZABLE) as a hint on a query. As for deadlock
it depends on what you want to do. A common situation is to read the
most recently generated id in a table to get the next id. In this
case the hint to use is UPDLOCK. If you only use SERIALIZABLE, you
get a dead if two processes tries to convert shared locks to exclusive.
Update locks are shared locks, but only process at a time can have
an Update lock on a resource.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
On Tue, 24 Aug 2004 09:24:41 -0400, Ross Presser wrote:

I am not an expert on this; someone else in the group may have better
information.


As I half expected, my knowledge was wrong. I've canceled my posting,
though it's probably too late.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1498
by: Rene Kadner | last post by:
Hi, Zu Test-/Entwicklungszwecken mache ich eine Rücksicherung von der produktiven MSSQL7 Datenbank auf einen MSSQL2000. Das klappte bisher immer Problemlos. Neuerdings erhalte ich aber aus...
1
1541
by: Inquisitive | last post by:
Hello! Does anybody know whether mssql2000 and emc mirrorvew _certified_ for joint work? (Mirrorview is a fc-based remote mirroring solution) I mean is it supported from the MS point of...
16
8898
by: Nid | last post by:
How do I do row-level locking on SQL Server? Thanks, Nid
0
1410
by: Marek Wierzbicki | last post by:
Drodzy czytelnicy listy Szukam specjalisty znaj±cego się perfekcyjne na MSSQL2000. Interesuje mnie wiedza typu "inside", czyli struktury plików itp. Konkretnie chodzi mi o organizacje wewnętrzn±...
0
1170
by: chowdhp | last post by:
Hi, I am trying to migrate our server MSSQL2000 to MSSQL2005 and getting the following errors. 1. When I tried to restore the Master DB in the test server(single user mode) from a backup file...
0
1311
by: xpding | last post by:
Hello, I have a class MyEmbededList contains a generic dictionary, the value field is actually the MyEmbededList type as well. There is another class need to access and manipulate a list of...
0
4086
by: Cindy Huyser | last post by:
I have an Access 2000 database behind a threaded Java application that that can have have concurrent access to the same table (but not the same record). The database is set up for shared access...
0
1219
by: comp21 | last post by:
Hi, Now, I have retrieved or imported data from excel spreadsheet to vb6 application. Now I want to compare this part of data with the one already existing in mssql2000 server(table name being...
3
1800
by: Erwin Moller | last post by:
Hi Group, I developed a intranet site using MSSQL7/win2000 some time ago. The target environment used MSSQL2000/8. We were (almost painlessly) able to import the db-scheme and data from 7 to 8....
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7114
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.