Solved

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

Posted on 2012-03-28
6
308 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

22 Experts available now in Live!

Get 1:1 Help Now