Solved

c# MSSQL next ID

Posted on 2010-11-08
3
374 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

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.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

948 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now