Solved

Sql Server 2005 Express Concurrent Users

Posted on 2010-09-02
4
424 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now