Solved

c# MSSQL next ID

Posted on 2010-11-08
3
377 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
[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
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

749 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