473,507 Members | 6,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to LOCK a table for INSERT and UPDATE, but not SELECT for duration of SqlTransaction?

Hi

I need to be able to lock a table against INSERT and UPDATE, but not
SELECT, for the duration of a transaction.
The transaction will be defined at the application level in c#, and
then use stored procedures to make multiple selects and then an
insert.
What is the best way of doing this?

Description of the system:

I am developing a scheduling system in c# with sql 2005.
The system allows users to book resources for a specified period of
time.
each booking may contain multiple resources, which cannot be involved
in multiple bookings for any one time frame.

When a new booking request is received, the app must select all booked
resources for the time frame specified in the received booking request
and check that the resources specified in the booking request are not
involved in any other booking in that time frame.
This will entail multiple selects, and iterating through each of the
result sets to be able to determine if a booking request is valid.

Oct 1 '07 #1
5 13506
A bit OT, but...

You can monkey with the isolation level of the transaction, and hints
like NOLOCK in the query, but you run the risk of non-repeatable reads
etc... serializable is the safest option, but introduces more locks.

To avoid escalation deadlocks (i.e. you query a range to check
validity, then if successful attempt to INSERT/UPDATE, only to find
you just deadlocked), you can issue an UPDLOCK hint on first SELECT to
ensure you have an exclusive lock sooner (rather than a shared lock).

The main thing, however, is to avoid long-running operations in the
middle of transactions; the main killers being things like user
confirmation dialogs (which don't help if the user has gone to lunch /
home while the transaction is alive).

Any help?

Marc
Oct 1 '07 #2
You just can't do this if the target of the SELECT, INSERT, and UPDATE
are the same. Placing a lock on one in a transaction is going to block out
access to the target rows (or pages, possibly) in another transaction. Like
Marc says, you can play with the isolation level, but you run the risk of
repeatable reads.

How long is your total operation taking? Are you seeing a good amount
of contention when you are performing your operations?

Are you loading all the data for your booking requests one-by-one, or
are you doing the check on the database server? You might be better off
with the latter because of the way that SQL Server handles set processing
(assuming the logic is transferrable).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"sticky" <st********@gmail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
Hi

I need to be able to lock a table against INSERT and UPDATE, but not
SELECT, for the duration of a transaction.
The transaction will be defined at the application level in c#, and
then use stored procedures to make multiple selects and then an
insert.
What is the best way of doing this?

Description of the system:

I am developing a scheduling system in c# with sql 2005.
The system allows users to book resources for a specified period of
time.
each booking may contain multiple resources, which cannot be involved
in multiple bookings for any one time frame.

When a new booking request is received, the app must select all booked
resources for the time frame specified in the received booking request
and check that the resources specified in the booking request are not
involved in any other booking in that time frame.
This will entail multiple selects, and iterating through each of the
result sets to be able to determine if a booking request is valid.

Oct 1 '07 #3
Hi,

Ina ddition to Marc's comments I suggest you to post this question in the
SQL NG

"sticky" <st********@gmail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
Hi

I need to be able to lock a table against INSERT and UPDATE, but not
SELECT, for the duration of a transaction.
The transaction will be defined at the application level in c#, and
then use stored procedures to make multiple selects and then an
insert.
What is the best way of doing this?

Description of the system:

I am developing a scheduling system in c# with sql 2005.
The system allows users to book resources for a specified period of
time.
each booking may contain multiple resources, which cannot be involved
in multiple bookings for any one time frame.

When a new booking request is received, the app must select all booked
resources for the time frame specified in the received booking request
and check that the resources specified in the booking request are not
involved in any other booking in that time frame.
This will entail multiple selects, and iterating through each of the
result sets to be able to determine if a booking request is valid.

Oct 1 '07 #4
On Oct 1, 6:27 am, sticky <stickym...@gmail.comwrote:
Hi

I need to be able to lock a table against INSERT and UPDATE, but not
SELECT, for the duration of a transaction.
The transaction will be defined at the application level in c#, and
then use stored procedures to make multiple selects and then an
insert.
What is the best way of doing this?

Description of the system:

I am developing a scheduling system in c# with sql 2005.
The system allows users to book resources for a specified period of
time.
each booking may contain multiple resources, which cannot be involved
in multiple bookings for any one time frame.

When a new booking request is received, the app must select all booked
resources for the time frame specified in the received booking request
and check that the resources specified in the booking request are not
involved in any other booking in that time frame.
This will entail multiple selects, and iterating through each of the
result sets to be able to determine if a booking request is valid.
If you have the database enforcing the "no resources multiply booked
at a time", say via an insert trigger, then do your selects separately
(perhaps combine them into a stored procedure to cutdown on
roundtrips). Then do your inserts/updates in a single transaction.
If the transaction fails, rollback and notify the user that there's a
resource conflict. Be sure to handle multirow inserts if applicable.
Oct 1 '07 #5
In SQLS 2005 you can configure the database to use snapshot isolation.
This creates a version of the row in tempdb that prevents readers from
blocking writers and writers from blocking readers. No locks are held
on the underlying tables during a select. You can enable it on a
per-database basis, or make it the default. You still get concurrency
conflicts during inserts and updates. See
http://msdn2.microsoft.com/en-us/library/ms189050.aspx for more
information.

-mary

On Mon, 01 Oct 2007 10:27:26 -0000, sticky <st********@gmail.com>
wrote:
>Hi

I need to be able to lock a table against INSERT and UPDATE, but not
SELECT, for the duration of a transaction.
The transaction will be defined at the application level in c#, and
then use stored procedures to make multiple selects and then an
insert.
What is the best way of doing this?

Description of the system:

I am developing a scheduling system in c# with sql 2005.
The system allows users to book resources for a specified period of
time.
each booking may contain multiple resources, which cannot be involved
in multiple bookings for any one time frame.

When a new booking request is received, the app must select all booked
resources for the time frame specified in the received booking request
and check that the resources specified in the booking request are not
involved in any other booking in that time frame.
This will entail multiple selects, and iterating through each of the
result sets to be able to determine if a booking request is valid.
Oct 3 '07 #6

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

Similar topics

12
9995
by: M Wells | last post by:
Hi All, I have a table that holds pregenerated member IDs. This table is used to assign an available member id to web site visitors who choose to register with the site So, conceptually the...
22
24627
by: Bryan Guilliams | last post by:
I'm trying to come up with an elegant, simple way to compare two consecutive values from the same table. For instance: SELECT TOP 2 datavalues FROM myTable ORDER BY timestamp DESC That...
0
1067
by: mahajan.sanjeev | last post by:
Hi All, I am using a SQLTransaction in a .Net application to insert records into a SQL Server table. At one time, there are 5000 or more records to be inserted one by one. It takes some 20-25...
4
8495
by: unkwb | last post by:
Dealing with the Oracle / DB2 XA (2-phase commit) drivers, I found a locking situation in DB2 I do not understand. It is actually a pretty simple scenario to which I can drill it down. Let say I...
4
4065
by: Ed L. | last post by:
I think I'm seeing table-level lock contention in the following function when I have many different concurrent callers, each with mutually distinct values for $1. Is there a way to reimplement...
2
15761
by: adri4n | last post by:
as wat ive mentioned in the title.. im would like to know whether the a particular record/table is being locked in my program. some of the methods which i would like to develop are as below: ...
16
3460
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
5
1905
by: sticky | last post by:
Hi I need to be able to lock a table against INSERT and UPDATE, but not SELECT, for the duration of a transaction. The transaction will be defined at the application level in c#, and then use...
1
3744
by: banging | last post by:
Hi there, I have a question regarding locking of tables so that when two or more people try to write or update the mysql tables, it locks up. Basically I only want one person to write to the...
0
7220
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
7105
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...
1
7023
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
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5617
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
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
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 ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
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.