Stored Procedure Thread Safe?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghd
    New Member
    • Sep 2007
    • 22

    Stored Procedure Thread Safe?

    Below i am producing a series of steps that creates a stored procedure. The stored procedure behaves like a sequence in oracle.

    The code below creates the table by name 'sequences'. This table will be used to hold the next value that can be used for a particular sequence
    Code:
                   CREATE TABLE sequences (
                     seq            varchar(100) primary key,
                     sequence_id    int
                   );
    The following code creates the stored procedure that gets the next value from the specified sequence in the 'sequences' table.

    Code:
                   CREATE PROCEDURE nextval
                     @sequence varchar(100),
                     @sequence_id INT OUTPUT
                   AS
    
                   -- return an error if sequence does not exist
                   -- so we will know if someone truncates the table
                   set @sequence_id = -1
                  
                   UPDATE sequences
                   SET    @sequence_id = sequence_id = sequence_id + 1
                   WHERE  seq = @sequence
    
                   RETURN @sequence_id
    Now my question is that if this procedure is run on the same database instance by multiple users simultaneously, will the procedure be 'synchronized' (that is will it run to completion for a user before starting to run for another one)?

    kind regards,

    ghd
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    NO, there is no synchronization between calls.

    Comment

    • ghd
      New Member
      • Sep 2007
      • 22

      #3
      How can we remedy the situation then? Does the sql server provide any solution?

      kind regards,

      ghd

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        Use trigger to cascade your updates and use transactions so that you can commit or rollback as necessary.

        Happy coding.

        -- CK

        Comment

        • ghd
          New Member
          • Sep 2007
          • 22

          #5
          Originally posted by ck9663
          Use trigger to cascade your updates and use transactions so that you can commit or rollback as necessary.

          Happy coding.

          -- CK
          But how do i detect that a race condition has occured and be able to correct the situation. Worst still, how do i notify the user(s) who have got the wrong value(s) due to such a race condition?

          kind regards

          ghd

          Comment

          • ghd
            New Member
            • Sep 2007
            • 22

            #6
            Originally posted by ghd
            But how do i detect that a race condition has occured and be able to correct the situation. Worst still, how do i notify the user(s) who have got the wrong value(s) due to such a race condition?

            kind regards

            ghd
            I think i need to use transaction isolation level. I will come back if that doesn't work.

            kind regards,

            ghd

            Comment

            Working...