Use sql affairs and finishing techniques

Use sql affairs and finishing techniques

Overview:

  In the actual project development, in order to ensure the consistency of the results of data manipulation and other requirements, the transaction is an essential tool for solving.

  According to SQLSERVER The principle, in fact, SQLSERVER execution of each statement is a transaction operation, which means that each SQL statement or operations are successful or failed operations: for example, the update statement, update multiple fields at the same time, does not appear some fields update is successful, and some field update failed.

  However, we usually process in the development process, said the transaction: in fact, refers to an ordered set of SQL collection, through a transaction to ensure consistency of the results of this set of SQL collection.

 

Transaction characteristics:

  The main characteristics of the transaction include: principle, consistency, isolation, durability

  1.  Atomic : A transaction must be a unit of work automatically, either all executed or not executed all.
  2.  Consistency : A transaction into the database from one consistent state to another consistent state, the end of the transaction, all the internal data are correct.
  3. Isolation : when multiple transactions concurrently executing a transaction is not affected by other transactions.
  4.  Persistence : After the transaction is committed, the data is permanent and can no longer be rolled back, and so is not affected by the shutdown event

Business classifieds:

  According to the implementation dimension affairs efforts, the transaction is divided into: automatic commit the transaction, explicit transaction, implicit transaction

   Auto commit the transaction : one of the default mechanism sqlserver, also known as their own affairs, this model each sql statement executed are based on the
   explicit transaction : that is what we usually often say Affairs, opened the transaction by Begin Transaction start, execute a set of SQL statements, commit the transaction by the Commit transaction, the end of the rollback transaction roll back the transaction.
   Implicit transaction : Set IMPLICIT_TRANSACTIONS ON using the implicit transaction mode is turned on, sql auto-commit is finished, when the end of a transaction, this mode is automatically enabled the next transaction, only Commit Transaction to commit a transaction, Rollback Transaction to roll back the transaction

   Show Transaction and implicit main difference from that implicit transaction automatically committed after the implementation.

Explicit transactions using the profile:

  Show Transaction open transaction begin Transaction, the transaction is rolled back by Rollback Transaction

  Data preparation, first create a table:  

---- create a table TEST_Name, each field is non-empty
CREATE TABLE [dbo].[TEST_Name](
	[Id] [int] NULL,
	[Name] [nvarchar](50) NULL
) ON [PRIMARY]

 

  Example:

---- normal complete execution of a transaction, and the transaction was normal SQL
- - Normal complete execution of a transaction, and SQL within a transaction without exception 
the begin  Tran 
INSERT  INTO test_name values ( 1 , 1 )
 INSERT  INTO test_name values ( 3 , 3 )
 the commit  Tran
---- execute a transaction, and the internal affairs SQL abnormal

the begin
Tran INSERT INTO test_name values ( 10 , 10 ) INSERT INTO test_name values ( 11 , null ) ---- times the statement fails, because the name can not be empty INSERT INTO test_name values ( 12 , 12 ) the commit Tran

- --- the results of the final statement is
successfully inserted with id: 10, 12 of the two data

  Through the above statements, such as the results of our affairs and consistency contrary, this is not what we want to see the use of the effect of the transaction, in fact, we hope that these three statements are inserted either succeed or fail is inserted

 To achieve the data, may be achieved by the following three ways: the try the catch; Results of determination, step by step execution, error rollback; XACT_ABORT opening (accurate termination)

  try catch implement transaction rollback

the begin  Tran 
the begin the try
     INSERT  INTO test_name values ( . 1 , . 1 )
     INSERT  INTO test_name values ( 2 , null )
     INSERT  INTO test_name values ( . 3 , 2 )
     the commit  Tran 
End the try
 the begin the catch
     SELECT  ' abnormality, transaction rollback ' 
    ROLLBACK  Tran 
End the catch 
---- execution result is: do not insert a data

  

   The results of judgment, step by step implementation of the rollback error

beginning  tran 
    declare  @error  int  
    set  @error = 0
    
    insert into TEST_name values(1,1)
    set @error=@error+@@error
    insert into TEST_name values(2,null)
    set @error=@error+@@error
    insert into TEST_name values(3,2)
    set @error=@error+@@error
    
IF ( @error <> 0 )
     the begin  
        SELECT  ' abnormality, transaction rollback ' 
        ROLLBACK  Tran 
    End 
the else  
    the begin 
        the commit  Tran 
    End

 

- - every step of the execution results are correct before continuing down the implementation of 
the begin  Tran 
    - - affect the number of rows 
    DECLARE  @ROWCOUNT  int 
    the SET  @ROWCOUNT = 0
    
    insert into TEST_name values(1,1)
    set @ROWCOUNT=@@ROWCOUNT
    if(@ROWCOUNT>0)
        begin 
            insert into TEST_name values(2,null)
            set @ROWCOUNT=@@ROWCOUNT
        end
        
    if(@ROWCOUNT>0)
        begin 
            insert into TEST_name values(3,2)
            set @ROWCOUNT=@@ROWCOUNT
        end
        
IF ( @ROWCOUNT <= 0 )
     the begin  
        SELECT  ' abnormality, transaction rollback ' 
        ROLLBACK  Tran 
    End 
the else  
    the begin 
        the commit  Tran 
    End

 

Open xact_abort (precise termination)
---- XACT_ABORT set up on: a statement on behalf of an execution error, do not continue down the execution and automatically roll back the transaction 
---- XACT_ABORT set off: a statement on behalf of an execution error, sub-article rollback statement is executed, and to continue the implementation of the follow-up statement, and submit successful execution of
  ---- off such a situation should be rarely used in the transaction, after all, the purpose of using the transaction is to achieve the consistency of the results of
the SET
XACT_ABORT oN the begin Tran INSERT INTO test_name values ( 10 , 10 ) INSERT iNTO test_name values ( 11 , null ) - - sub statement fails because the name can not be empty INSERT iNTO test_name values ( 12 , 12 ) the commit Tran

 

Set transaction savepoint:

  In the normal course of affairs, there may need to implement, when the transaction is rolled back, only to roll back to the specified location, the results before the specified location is not rolled back

  In sqlserver by saving the transaction point, to achieve accurate roll back of the transaction, keywords are: Save Transaction and rollback transaction, specifically using the following rules:

 

- - every step of the execution results are correct before continuing down the implementation of 
the begin  Tran 
    - - affect the number of rows 
    DECLARE  @ROWCOUNT  int 
    the SET  @ROWCOUNT = 0
    
    insert into TEST_name values(1,1)
    set @ROWCOUNT=@@ROWCOUNT
    save tran stanstation1
    --- save tran transtation1
    if(@ROWCOUNT>0)
        begin 
            insert into TEST_name values(2,null)
            set @ROWCOUNT=@@ROWCOUNT
        end
        
    if(@ROWCOUNT>0)
        begin 
            insert into TEST_name values(3,2)
            set @ROWCOUNT=@@ROWCOUNT
        end
        
IF ( @ROWCOUNT <= 0 )
     the begin  
        SELECT  ' abnormality, transaction rollback ' 
        - - the execution result is: 1,1 successfully inserted into a database table 
        ROLLBACK  Tran stanstation1
     End 
the else  
    the begin 
        SELECT  ' transaction commit ' 
        the commit  Tran 
    End

 

 to sum up:

  Through the above review, combined with exercise, the business-to-sql have a better understanding. Simple conclusion: virtually every sql execution are based on transactions to achieve, in actual use, we generally use the show to handle business affairs, but in the course of a transaction must be combined with the corresponding policies to ensure transaction execution consistency of results.

  Wrote this today, then tomorrow it simple comb to sum up implementation of a distributed transaction, this module is also very important, especially in today's large systems, when the sub-library sub-table, a distributed transaction is very useful







xact_abort

Guess you like

Origin www.cnblogs.com/xiaoXuZhi/p/xyh_trans_conclude.html