Solved

c# MSSQL next ID

Posted on 2010-11-08
3
375 Views
Last Modified: 2012-08-14
I have a web service which enters data in an SQL table. Website generates a request for the web service and gets an ID back immediately. Then the web service performs some actions and at last it enters the data with this id in the table. So I have to get the next possible id in the table, retur it to the website, perform some actions then insert the record with this id in the table.

Can you please tell me how I can get sure, that this ID will not be used by another thread? It seams insecure just to get the ID and then insert the record, because the actions which the service performs in between take quite some time. Can I look the table for this time? Will this influence the performance?
0
Comment
Question by:socom1985
  • 3
3 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 34082428
rather locking the table you can Mutex as synchronization mechanism to ensure that only one thread at a time calls the method which returns next available ID.releasing the Mutex should take place only after the ID was used when inserting the record.then when the next thread asks for next available ID, the previous ID won't be returned cause it was always inserted with the record by the last thread.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 34082441
from http://msdn.microsoft.com/en-us/library/ms173179%28VS.80%29.aspx:Although a mutex can be used for intra-process thread synchronization, using Monitor is generally preferred, because monitors were designed specifically for the .NET Framework and therefore make better use of resources. In contrast, the Mutex class is a wrapper to a Win32 construct. While it is more powerful than a monitor, a mutex requires interop transitions that are more computationally expensive than those required by the Monitor class.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 34082445
here's a sample code:System.Object obj = (System.Object)x;System.Threading.Monitor.Enter(obj);try{int id = GetNextAvailaleID();InsertRecord(id, someRecord);}finally{    System.Threading.Monitor.Exit(obj);}
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

808 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