Solved

Sql Server 2005 Express Concurrent Users

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
No row return after calling the fillschema method 4 40
Visual Studio editor? 1 38
How to write a sql query returning a result  with my date time template? 8 33
Sql query 107 30
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

863 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

25 Experts available now in Live!

Get 1:1 Help Now