Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Atomic Sql Statements (Transaction)

Atomic Sql Statements (Transaction)

Scheduled Pinned Locked Moved Database
databasehelptutorialquestioncom
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Meysam Mahfouzi
    wrote on last edited by
    #1

    I read an article here[^] describing how to perform SQL operations in an atomic manner using transactions. I just do not understand what the usage of a transaction is in the following example (I didn't ask my question below that article because the author has not answered asked questions).

    CREATE PROCEDURE addTitle(@title_id VARCHAR(6), @au_id VARCHAR(11),
    @title VARCHAR(20), @title_type CHAR(12))
    AS

    BEGIN TRAN
    INSERT titles(title_id, title, type)
    VALUES (@title_id, @title, @title_type)

    IF (@@ERROR <> 0) GOTO ERR\_HANDLER
    
    INSERT titleauthor(au\_id, title\_id)
    VALUES (@au\_id, @title\_id)
    
    IF (@@ERROR <> 0) GOTO ERR\_HANDLER
    

    COMMIT TRAN

    RETURN 0

    ERR_HANDLER:
    PRINT 'Unexpected error occurred!'
    ROLLBACK TRAN
    RETURN 1

    My question is, if the the first insert statement failed, why do we need to roll back it? No insertion to database happened, therefore we do not need to rollback anything! If I'm right, what's the problem with the following sproc:

    CREATE PROCEDURE addTitle(@title_id VARCHAR(6), @au_id VARCHAR(11),
    @title VARCHAR(20), @title_type CHAR(12))
    AS

    INSERT titles(title\_id, title, type)
    VALUES (@title\_id, @title, @title\_type)
    
    IF (@@ERROR <> 0) GOTO ERR\_HANDLER
    
    INSERT titleauthor(au\_id, title\_id)
    VALUES (@au\_id, @title\_id)
    
    IF (@@ERROR <> 0) GOTO ERR\_HANDLER
    
    RETURN 0
    

    ERR_HANDLER:
    RETURN 1

    J M W 3 Replies Last reply
    0
    • M Meysam Mahfouzi

      I read an article here[^] describing how to perform SQL operations in an atomic manner using transactions. I just do not understand what the usage of a transaction is in the following example (I didn't ask my question below that article because the author has not answered asked questions).

      CREATE PROCEDURE addTitle(@title_id VARCHAR(6), @au_id VARCHAR(11),
      @title VARCHAR(20), @title_type CHAR(12))
      AS

      BEGIN TRAN
      INSERT titles(title_id, title, type)
      VALUES (@title_id, @title, @title_type)

      IF (@@ERROR <> 0) GOTO ERR\_HANDLER
      
      INSERT titleauthor(au\_id, title\_id)
      VALUES (@au\_id, @title\_id)
      
      IF (@@ERROR <> 0) GOTO ERR\_HANDLER
      

      COMMIT TRAN

      RETURN 0

      ERR_HANDLER:
      PRINT 'Unexpected error occurred!'
      ROLLBACK TRAN
      RETURN 1

      My question is, if the the first insert statement failed, why do we need to roll back it? No insertion to database happened, therefore we do not need to rollback anything! If I'm right, what's the problem with the following sproc:

      CREATE PROCEDURE addTitle(@title_id VARCHAR(6), @au_id VARCHAR(11),
      @title VARCHAR(20), @title_type CHAR(12))
      AS

      INSERT titles(title\_id, title, type)
      VALUES (@title\_id, @title, @title\_type)
      
      IF (@@ERROR <> 0) GOTO ERR\_HANDLER
      
      INSERT titleauthor(au\_id, title\_id)
      VALUES (@au\_id, @title\_id)
      
      IF (@@ERROR <> 0) GOTO ERR\_HANDLER
      
      RETURN 0
      

      ERR_HANDLER:
      RETURN 1

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      In your version if the second insert fails, then an orphaned title has been added without its associated author.

      M 1 Reply Last reply
      0
      • M Meysam Mahfouzi

        I read an article here[^] describing how to perform SQL operations in an atomic manner using transactions. I just do not understand what the usage of a transaction is in the following example (I didn't ask my question below that article because the author has not answered asked questions).

        CREATE PROCEDURE addTitle(@title_id VARCHAR(6), @au_id VARCHAR(11),
        @title VARCHAR(20), @title_type CHAR(12))
        AS

        BEGIN TRAN
        INSERT titles(title_id, title, type)
        VALUES (@title_id, @title, @title_type)

        IF (@@ERROR <> 0) GOTO ERR\_HANDLER
        
        INSERT titleauthor(au\_id, title\_id)
        VALUES (@au\_id, @title\_id)
        
        IF (@@ERROR <> 0) GOTO ERR\_HANDLER
        

        COMMIT TRAN

        RETURN 0

        ERR_HANDLER:
        PRINT 'Unexpected error occurred!'
        ROLLBACK TRAN
        RETURN 1

        My question is, if the the first insert statement failed, why do we need to roll back it? No insertion to database happened, therefore we do not need to rollback anything! If I'm right, what's the problem with the following sproc:

        CREATE PROCEDURE addTitle(@title_id VARCHAR(6), @au_id VARCHAR(11),
        @title VARCHAR(20), @title_type CHAR(12))
        AS

        INSERT titles(title\_id, title, type)
        VALUES (@title\_id, @title, @title\_type)
        
        IF (@@ERROR <> 0) GOTO ERR\_HANDLER
        
        INSERT titleauthor(au\_id, title\_id)
        VALUES (@au\_id, @title\_id)
        
        IF (@@ERROR <> 0) GOTO ERR\_HANDLER
        
        RETURN 0
        

        ERR_HANDLER:
        RETURN 1

        M Offline
        M Offline
        Meysam Mahfouzi
        wrote on last edited by
        #3

        I guess I found my answer, it's useful when the first statement succeeds and the second one doesn't. At that time we need to rollback the first successful statement.

        1 Reply Last reply
        0
        • J J4amieC

          In your version if the second insert fails, then an orphaned title has been added without its associated author.

          M Offline
          M Offline
          Meysam Mahfouzi
          wrote on last edited by
          #4

          You are right, I just figured it out :) Thank you

          _

          1 Reply Last reply
          0
          • M Meysam Mahfouzi

            I read an article here[^] describing how to perform SQL operations in an atomic manner using transactions. I just do not understand what the usage of a transaction is in the following example (I didn't ask my question below that article because the author has not answered asked questions).

            CREATE PROCEDURE addTitle(@title_id VARCHAR(6), @au_id VARCHAR(11),
            @title VARCHAR(20), @title_type CHAR(12))
            AS

            BEGIN TRAN
            INSERT titles(title_id, title, type)
            VALUES (@title_id, @title, @title_type)

            IF (@@ERROR <> 0) GOTO ERR\_HANDLER
            
            INSERT titleauthor(au\_id, title\_id)
            VALUES (@au\_id, @title\_id)
            
            IF (@@ERROR <> 0) GOTO ERR\_HANDLER
            

            COMMIT TRAN

            RETURN 0

            ERR_HANDLER:
            PRINT 'Unexpected error occurred!'
            ROLLBACK TRAN
            RETURN 1

            My question is, if the the first insert statement failed, why do we need to roll back it? No insertion to database happened, therefore we do not need to rollback anything! If I'm right, what's the problem with the following sproc:

            CREATE PROCEDURE addTitle(@title_id VARCHAR(6), @au_id VARCHAR(11),
            @title VARCHAR(20), @title_type CHAR(12))
            AS

            INSERT titles(title\_id, title, type)
            VALUES (@title\_id, @title, @title\_type)
            
            IF (@@ERROR <> 0) GOTO ERR\_HANDLER
            
            INSERT titleauthor(au\_id, title\_id)
            VALUES (@au\_id, @title\_id)
            
            IF (@@ERROR <> 0) GOTO ERR\_HANDLER
            
            RETURN 0
            

            ERR_HANDLER:
            RETURN 1

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #5

            There are several considerations in both examples. Some of them are: 1. Transaction usage and scope: In the first example transaction is started and ended inside stored proc. Personally I don't feel this is a good way to handle transactions. Consider what would happen if the real world situation involves call to two stored procs. If both of them autonomously end transaction, there's no quarantee that the result is correct. This is why I always start and end transactions at calling side. 2. Why rollback if first statement fails: Even though the insert itself fails, transaction is started and DML is executed. This has resulted to resource usage. The only way to free those resources is to roll back the transaction (since there's no point to commit a failure). So basically I would prefer the second version of the stored proc, but I also would ensure that it's executed inside a transaction. This would make it possible to have larger scope transactions and also to retry failed operations inside a transaction which is a very elemental requirement in certain system types. Mika

            The need to optimize rises from a bad design. My articles[^]

            P 1 Reply Last reply
            0
            • W Wendelius

              There are several considerations in both examples. Some of them are: 1. Transaction usage and scope: In the first example transaction is started and ended inside stored proc. Personally I don't feel this is a good way to handle transactions. Consider what would happen if the real world situation involves call to two stored procs. If both of them autonomously end transaction, there's no quarantee that the result is correct. This is why I always start and end transactions at calling side. 2. Why rollback if first statement fails: Even though the insert itself fails, transaction is started and DML is executed. This has resulted to resource usage. The only way to free those resources is to roll back the transaction (since there's no point to commit a failure). So basically I would prefer the second version of the stored proc, but I also would ensure that it's executed inside a transaction. This would make it possible to have larger scope transactions and also to retry failed operations inside a transaction which is a very elemental requirement in certain system types. Mika

              The need to optimize rises from a bad design. My articles[^]

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Re: your point 1 The only time I've written stored procedures was when I was using ad hoc ODBC statements (about ten years ago) and had no other way to do transactions. Currently, with ADO.net, I can do transactions in my program so I don't use stored procedures at all. However, a properly designed system of stored procedures should provide everything the application needs, including transactioning. The application shouldn't need to know about the details of the schema and what the stored procedures are doing, and therefore should be able to just execute the stored procedures without thought to whether or not to use transactions.

              W 1 Reply Last reply
              0
              • P PIEBALDconsult

                Re: your point 1 The only time I've written stored procedures was when I was using ad hoc ODBC statements (about ten years ago) and had no other way to do transactions. Currently, with ADO.net, I can do transactions in my program so I don't use stored procedures at all. However, a properly designed system of stored procedures should provide everything the application needs, including transactioning. The application shouldn't need to know about the details of the schema and what the stored procedures are doing, and therefore should be able to just execute the stored procedures without thought to whether or not to use transactions.

                W Offline
                W Offline
                Wendelius
                wrote on last edited by
                #7

                I think there are several factors that affect the choice how transactions should be used. In my example I used two separate procedures that are executed in a sequence. If the execution as a whole should have ACID properties, the must be wrapped inside a common transaction. Basically this gives two options: 1. create a new stored procedure combining both calls thus leading to single call to backend 2. use transaction with a scope over two different SQL calls In point 1, transaction problem is solved (in a simple case) but also the backend now knows (at least partly) the business logic (order of operations and the sequence). This may or may not be acceptable. The problem becomes more difficult if the procedures are not located in the same backend In point 2, we would need a higher level transaction manager (such as classes in System.Transactions namespace or even self made mechanism). This adds extra 'overhead' to the programming but also it's possible to have more resources inside the same transactions. If this is needed, is again depending on the requirements of the system. One good link that came in mind: http://blogs.msdn.com/diegumzone/archive/2006/08/14/699219.aspx[^]

                The need to optimize rises from a bad design. My articles[^]

                P 1 Reply Last reply
                0
                • W Wendelius

                  I think there are several factors that affect the choice how transactions should be used. In my example I used two separate procedures that are executed in a sequence. If the execution as a whole should have ACID properties, the must be wrapped inside a common transaction. Basically this gives two options: 1. create a new stored procedure combining both calls thus leading to single call to backend 2. use transaction with a scope over two different SQL calls In point 1, transaction problem is solved (in a simple case) but also the backend now knows (at least partly) the business logic (order of operations and the sequence). This may or may not be acceptable. The problem becomes more difficult if the procedures are not located in the same backend In point 2, we would need a higher level transaction manager (such as classes in System.Transactions namespace or even self made mechanism). This adds extra 'overhead' to the programming but also it's possible to have more resources inside the same transactions. If this is needed, is again depending on the requirements of the system. One good link that came in mind: http://blogs.msdn.com/diegumzone/archive/2006/08/14/699219.aspx[^]

                  The need to optimize rises from a bad design. My articles[^]

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  I suppose two phase commit would be another reason to avoid stored procedures and just write the stuff in code. :-D

                  W 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    I suppose two phase commit would be another reason to avoid stored procedures and just write the stuff in code. :-D

                    W Offline
                    W Offline
                    Wendelius
                    wrote on last edited by
                    #9

                    Might be :-D

                    The need to optimize rises from a bad design. My articles[^]

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups