Solved

Sql Server 2005 Express Concurrent Users

Posted on 2010-09-02
4
439 Views
Last Modified: 2013-12-25
I have a project that I'm developing written in VB.NET 2005 against a SQL Server 2005 Express database.  There may be up to 25 users accessing the database at the same time – very unlikely, but possible.  

The software is for issuing municipal burning permits for local residents.  Each permit must have a unique PermitNo that I can calculate from accessing the last record in the tblPermits table.  The PermitID is nothing special; it’s just the last two digits of the current year, followed by the number of permits issued.

For example, if the last permit issued had a permit ID of 10-3042 that would indicate the permit was issued in the year 2010, and was the 3,042 permit issued that year.  The next permit issued would be 10-3043.  It’s no problem calculating this in vb.net code - using query's against the SQL server.

My question is how I should handle if two of the 25 concurrent users want to issue a burn permit at the exact same time?  Very unlikely I know, but how can I prevent conflicts?  The PermitNo field is indexed but should have no duplicates.
0
Comment
Question by:DRRobinson
  • 2
4 Comments
 
LVL 7

Expert Comment

by:rashmi_vaghela
ID: 33593656
you can create stored procedure having output as perimit id which can prevent conflict
for eg:
select Substring(cast(REPLACE('10-3042','-','')+1 as  varchar(10)),0,3)+Substring(cast(REPLACE('10-3042','-','')+1 as  varchar(10)),3,LEN('10-3042')-3)

you can put in stored proc and it will generate new permit id every time for users
0
 

Author Comment

by:DRRobinson
ID: 33597497
So a stored procedure cannot be run twice at the exact same time?  I now have a stored procedure that will return the last used permit number, but if I modify that to not just return the last permit number, but to insert a record into the table, that will prevent any conflicts?  
0
 
LVL 7

Accepted Solution

by:
lundnak earned 500 total points
ID: 33599042
DRRobinson: Stored procedures can be executed simultaneously by multiple users at the same time.

A method I use is the following:

1. Create a table to hold the permit numbers.
   Create Table permit_tbl
      (year int,
       permit_number int)
   Insert into permit_tbl select 2010,0

2. Create a stored procedure or function to get the next permit number.
   Create procedure get_new_permit_number @year int, @permit_number @varchar(20) output
      as
      begin
         Begin Transaction
            Update permit_tbl
               Set permit_number = permit_number + 1
               Where year = @year
            Select @permit_number = convert(varchar,year) + '-' + convert(varchar,permit_number)
               From Permit_tbl
               Where year = @year
         Commit Transaction
      end

3. Call the stored procedure to get the new permit number.  The update statement coupled with the "transaction" will lock the row from another update until the commit is done.  If someone else calls the stored procedure for the same year they will wait until the lock is released.

Declare @permit_number = varchar(20)
exec get_new_permit_number @year=2010, @permit_number  output
select @permit_number
0
 

Author Closing Comment

by:DRRobinson
ID: 33599221
Thanks for the detailed explanation
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

816 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

10 Experts available now in Live!

Get 1:1 Help Now