Solved

Sql Server 2005 Express Concurrent Users

Posted on 2010-09-02
4
455 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
[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
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

756 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