Solved

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

Posted on 2012-03-28
6
312 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
[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
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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 40

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help with a Stored Proc on Sql Server 2012 4 35
MS SQL Server select from Sub Table 14 42
Enabled trace flag 4135 or 4199 - SQL SERVER 2 24
SQL Select Query help 1 38
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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