Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Table locking with nested sets?

Posted on 2008-10-09
2
Medium Priority
?
325 Views
Last Modified: 2012-08-14
I'm attempting to use nested sets to represent a data hierarchy (see: http://dev.mysql.com/tech-resources/articles/hierarchical-data.html, except I'm doing it in SQL 2000, not MySQL).

I've written a stored procedure that handles an insert, it looks something like this the simplified code snippet I've attached.  It works fine, but my concern is about concurrent users (i.e. someone else updating/inserting and affecting the hierarchy at the same time).  The MySQL example uses a lock on the what I can only assume is the whole table before doing anything, but this seems like it might be
dangerous and/or overkill, especially if this data is ultimately being viewed a lot on a public web site.  I thought about just creating a seperate table with one field and one row to act as a lock in this stored procedure (and potentially a couple other related SPs), but that doesn't seem like a best practice.

Any thoughts?

CREATE PROCEDURE [dbo].[INSERT_NESTED_SET_WEBPAGE]
	@varTitle varchar(256),
	@parent int
AS
 
-- get the parent's rgt value
DECLARE @parentRight int;
SELECT @parentRight = rgt FROM my_data WHERE ID = @parent
 
-- make room for the new child (at the end of the nested set)
UPDATE my_data SET lft = lft + 2 WHERE lft >= @parentRight
UPDATE my_data SET rgt = rgt + 2 WHERE rgt >= @parentRight
 
-- insert the new child (at the end of the nested set)
INSERT INTO my_date (title, lft, rgt) VALUES (
	@varTitle,
	@parentRight,
	@parentRight + 1)
 
GO

Open in new window

0
Comment
Question by:saitwebdev
[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 Comments
 
LVL 14

Accepted Solution

by:
puranik_p earned 1000 total points
ID: 23054534
Just put the whole thing in a transaction. That's it!
The transactions are the simplest way to take care of the concurrency.
CREATE PROCEDURE [dbo].[INSERT_NESTED_SET_WEBPAGE]
        @varTitle varchar(256),
        @parent int
AS
 
-- get the parent's rgt value
DECLARE @parentRight int;
BEGIN TRANSACTION
SELECT @parentRight = rgt FROM my_data WHERE ID = @parent
 
-- make room for the new child (at the end of the nested set)
UPDATE my_data SET lft = lft + 2 WHERE lft >= @parentRight
UPDATE my_data SET rgt = rgt + 2 WHERE rgt >= @parentRight
 
-- insert the new child (at the end of the nested set)
INSERT INTO my_date (title, lft, rgt) VALUES (
        @varTitle,
        @parentRight,
        @parentRight + 1)
 
COMMIT TRANSACTION
GO

Open in new window

0
 

Author Closing Comment

by:saitwebdev
ID: 31504784
Thank you.  I came to that conclusion some time ago, but it's good to have confirmation.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

604 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