Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sql Server 2005 Express Concurrent Users

Posted on 2010-09-02
4
Medium Priority
?
473 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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