Solved

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

Posted on 2012-03-28
6
306 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

14 Experts available now in Live!

Get 1:1 Help Now