473,469 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Serializable transactions and insert.

Hi,

I have a problem using serializable transactions. In one transaction I
select a single specific row from a table using a where clause (this
causes it to acquire and hold a range lock). In another transaction I
attempt to insert a new row into the table (which does not match the
first transactions where clause), but it is blocked by the first
transaction. The reading I have done on SQL Server suggests that I
should be able to insert rows, as long as the new rows do not match the
where clause in the other transactions select.

Here is what I do:

Transaction 1
SET TRANSACTION ISOLATION LEVEL Serializable
BEGIN TRANSACTION
select * from test_table where id=1;

and then on another session I run
Transaction 2

SET TRANSACTION ISOLATION LEVEL Serializable
BEGIN TRANSACTION
insert into test_table values (2, 'two');

Transaction 2 cannot complete until transaction 1 has finished. I have
tried using WITH (ROWLOCK) hints but to no avail. Am I missing
something important? Is this true of other DBs?

(I am able to update rows that are not in transaction 1's where clause)

I am using SQL Server version 8.00.760(SP3).

Thanks for your help,

Magnus.

Jul 23 '05 #1
3 5751
What you saw is probably the behavior by design. Looks like the index is
built on column id but not unique. The range lock taken under serializable
isolation level is acquired on the next key value outside the range.
Suppose the rows in the table look like (1, 'one'), (3, 'three'), (4,
'four'). For this select query the range lock is obtained on row (3,
'three') even if only the row (1, 'one') qualifies for the query. This is
to prevent new inserts (qualifying the predicate) after the last row
returned for the query. Now, if the index is unique on the column id, then
the select query will not obtain a range lock and you won't have this
problem.

--
Gang He
Software Design Engineer
Microsoft SQL Server Storage Engine

This posting is provided "AS IS" with no warranties, and confers no rights.
"Magnus Byne" <Ma********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

I have a problem using serializable transactions. In one transaction I
select a single specific row from a table using a where clause (this
causes it to acquire and hold a range lock). In another transaction I
attempt to insert a new row into the table (which does not match the
first transactions where clause), but it is blocked by the first
transaction. The reading I have done on SQL Server suggests that I
should be able to insert rows, as long as the new rows do not match the
where clause in the other transactions select.

Here is what I do:

Transaction 1
SET TRANSACTION ISOLATION LEVEL Serializable
BEGIN TRANSACTION
select * from test_table where id=1;

and then on another session I run
Transaction 2

SET TRANSACTION ISOLATION LEVEL Serializable
BEGIN TRANSACTION
insert into test_table values (2, 'two');

Transaction 2 cannot complete until transaction 1 has finished. I have
tried using WITH (ROWLOCK) hints but to no avail. Am I missing
something important? Is this true of other DBs?

(I am able to update rows that are not in transaction 1's where clause)

I am using SQL Server version 8.00.760(SP3).

Thanks for your help,

Magnus.

Jul 23 '05 #2
Hi,

Thanks for your help. The situation I have is where a number of threads
are adding things to a table. Inside a transaction they first do a
select to make sure someone has not already inserted the data - so
typically the first select returns an empty rowset. Selecting an empty
rowset (e.g. key does not exist) always seems to create a Key Range
lock regardless of the indexes on the table (or a table lock if there
are no indexes). Is there anyway to around this?

Many thanks,
Magnus.

Jul 23 '05 #3
Magnus Byne (Ma********@gmail.com) writes:
Thanks for your help. The situation I have is where a number of threads
are adding things to a table. Inside a transaction they first do a
select to make sure someone has not already inserted the data - so
typically the first select returns an empty rowset. Selecting an empty
rowset (e.g. key does not exist) always seems to create a Key Range
lock regardless of the indexes on the table (or a table lock if there
are no indexes). Is there anyway to around this?


I played around a little, and for once it seems that Gang was wrong.

CREATE TABLE testie (a int NOT NULL PRIMARY KEY,
somedata varchar(500) NOT NULL)
go
INSERT testie (a, somedata)
VALUES (1, replicate('x', 243))
INSERT testie (a, somedata)
VALUES (3, replicate('x', 243))
INSERT testie (a, somedata)
VALUES (5, replicate('x', 243))
INSERT testie (a, somedata)
VALUES (10, replicate('x', 243))
INSERT testie (a, somedata)
VALUES (19, replicate('x', 243))

Then I ran in one query window:

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
IF ENOT XISTS (SELECT * FROM testie WHERE a = 9)
INSERT testie (a, somedata) VALUES (9, replicate('l', 23))

Note: no COMMIT or ROLLBACK!

Then in a second window, I did:

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
IF EXISTS (SELECT * FROM testie WHERE a = 8)
INSERT testie (a, somedata) VALUES (8, replicate('l', 23))

This blocked. However, if I changed 8 to 4, the second query did not
block. So it appears that even if the key is unique, there is a
range lock. And when you think of it, there is not much to do. If
there is no row, what should SQL Server lock on? Thin air?
--
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 23 '05 #4

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

Similar topics

0
by: Fabrice FRASSAINT | last post by:
Hello, Does someone knows how transactions are made in MySQL on Berkeley DB type tables ??? I need to insert a new entry in a table but one of its field is an order field so i use the MAX of...
7
by: Richard Maher | last post by:
Hi, I am seeking the help of volunteers to test some software that I've developed which facilitates distributed two-phase commit transactions, encompassing any resource manager (e.g. SQL/Server...
1
by: D. Dante Lorenso | last post by:
I just wrote a PL/PGSQL function that is working, but I don't know why it is... I have a foreign key constraint defined on: transaction.invoice_id --> invoice.invoice_id But I did NOT state...
9
by: TD | last post by:
I am trying to add transactions to my code. The original code worked fine until I followed an example to setup transactions, now the code does strange things, but no error messages. Could...
2
by: Adnan | last post by:
Hey Ppl, I'm developing an Online Auction Site using ASP.net and am experiencing a problem with Transactions in ADO.Net. When beginTrasaction() function is invoked from a specific connection,...
1
by: John Wells | last post by:
On this page: http://www.compiere.org/technology/independence.html, the project leader of Compiere (a popular ERP package) states that the move to Postgres failed because of lack of support of...
0
by: Ben Dewey | last post by:
Guys, I am new to the System.Transactions namespace, but I am trying to use it to process powerpoint document into a database. I have a strongly typed DataSet with Talks and Slides. Both...
2
by: Sridhar | last post by:
Hi, I am trying to implement sql transactions. But I am not knowing how to do that. I created a data access layer which contains methods to select/insert/update tables in a database. I have also...
12
by: Rami | last post by:
I have some requirement for an automated payment system. The system has four machines setup as follows: 1- Two machines have a clustered database. 2- Two machines have a .net business logic...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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
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 ...

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.