Solved

Handling concurrency issues while inserting a record in to the sqlserver DB

Posted on 2012-03-28
6
310 Views
Last Modified: 2012-03-29
My db is sql2008r2

I have an issue that the same stored procedure is being invoked at exactly the same time with exactly the same paramenters.


The purpose of the stored procedure is  fetch a last patientno and increment it by one   and insert the new record

The problem is that if user A is initiated the insert operation and fetched the last patientno (say 001) and incremented it by one and by the time user A completes the insert and the new record is inserted  (id 002), another user has completed the  insert operation. now if the user A succeds in insert oprations with its incremented value of  id , it will create duplicate record .

How to avoid this scenario


Any help would be appreciated,
0
Comment
Question by:venkataramanaiahsr
6 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 37777347
There are different options, my basic question is why don't you use an identity column for
patientno
0
 

Author Comment

by:venkataramanaiahsr
ID: 37777386
The patient no is ten digit no which gets reset every calender year. for eg for year 2011 , it starts from 11-0000001 to ...... . for 2012 it starts from 12-0000001 to ...... .

before inserting the next number i have incorporate the logic to have the number converted to this format.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37777474
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 39

Expert Comment

by:lcohan
ID: 37778026
Just be extremely cautios with explicit transactions that can introduce other issues as locking/blocking and the worst of all - deadlocks. Keep the explicit transaciotn (betwwen begin and commit) to the minimum or...use try/catch instead:

http://msdn.microsoft.com/en-us/library/ms175976(v=sql.105).aspx
0
 

Author Comment

by:venkataramanaiahsr
ID: 37780191
i would like your  advide on another point.  Which is better of the  following two options

1. using connection object begintran, committrans and rollback trans and just using simple insert statment in storedprocedure and using this proc in connection/recordset objects execute trans  beween its begin and committrans.

2. using explicit trans in stored procedure  and just executing the sp inside the code using connection object or recordset object.
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37780321
here us a result of discussion:-

If you need to update one record or need to execute single SQL statement, you do not need to open transaction at all, since single operations are always atomic. To open transaction from the code or in stored procedure will have same result. Everything depends on what you need to achieve from your code. If you need to call multiple stored procedures from your code inside of single transaction then you would open transaction from inside of your code, so you maintain and control it from central point. If the whole transaction "fits" into functionality of single stored procedure then you could use both ways

at
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/dfaba053-132d-4bcc-9828-45697b0a9319/
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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