290 likes | 383 Views
Transactions. What are transactions. Often called a Unit of Work (UOW) It is a block of code that must succeed or fail as a single unit. ACID Test. Actions must pass the ACID test to be classified as a transaction Atomicity Consistency Isolation Durability. Atomicity.
E N D
What are transactions • Often called a Unit of Work (UOW) • It is a block of code that must succeed or fail as a single unit
ACID Test • Actions must pass the ACID test to be classified as a transaction • Atomicity • Consistency • Isolation • Durability
Atomicity • All data modifications must be both accepted and inserted or none of the modifications will be performed
Consistency • Once data has been successfully applied or rolled back, all the data must remain in a consistent state
Isolation • Any modification in one transaction must be isolated from any modifications in any other transaction. • A transaction must see the data from another transaction in its original state, or its completed state
Durability • Once a transaction has finished, all its data modifications are in place and can only be changed by another transaction
How are they defined • Started by a BEGIN TRANSACTION • Completed by a • COMMIT • ROLLBACK TRANSACTION
Commit • Makes any changes done to a database permanent
Rollback Transaction • A ROLLBACK TRANSACTION undoes any changes made to the database since the BEGIN TRANSACTION • It leaves the database as if nothing had occurred
Program Responsibility • It is generally the program’s responsibility to determine if a COMMIT or ROLLBACK should occur • This can be determined by program logic • It can be for business rules reasons • It can be for error conditions
Error Conditions • @@ERROR • XACT_ABORT
@@ERROR • @@ERROR is a system variable that contains the return code for a SQL statement • A return code of 0 signifies a successful statement • A return code < 0 signifies an error • A return code > 0 signifies an informational message
Example of @@ERROR BEGIN TRANSACTION INSERT c VALUES ('X') IF (@@ERROR <> 0) GOTO on_error INSERT b VALUES ('X') -- Fails reference IF (@@ERROR <> 0) GOTO on_error COMMIT TRANSACTION RETURN(0) on_error; ROLLBACK TRANSACTION
XACT_ABORT • Also used for error checking • Terminates the batch without a COMMIT • Does an implicit ROLLBACK
Example of XACT_ABORT SET XACT_ABORT ON BEGIN TRANSACTION INSERT c VALUES ('X') INSERT b VALUES ('X') -- Fails reference COMMIT TRANSACTION
Notes about XACT_ABORT • It accomplishes the same thing, but obviously doing your own checking gives you better control over how the batch behaves. • In a professional environment, it is generally best to check @@ERROR and write code to handle the error. Additional code should be added to record the error condition for debugging purposes.
Transaction Isolation Level • Controls the kind of locking that is done on a row when it is being read in the database • Read Uncommitted • Read Committed • Repeatable Read • Serializable
Read Uncommitted Implements dirty read, or isolation level 0 locking, which means that no shared locks are issued and no exclusive locks are honored. When this option is set, it is possible to read uncommitted or dirty data; values in the data can be changed and rows can appear or disappear in the data set before the end of the transaction. This option has the same effect as setting NOLOCK on all tables in all SELECT statements in a transaction. This is the least restrictive of the four isolation levels.
Read Committed Specifies that shared locks are held while the data is being read to avoid dirty reads, but the data can be changed before the end of the transaction, resulting in nonrepeatablereadsor phantom data. This option is the SQL Server default.
Repeatable Read Locks are placed on all data that is used in a query, preventing other users from updating the data, but new phantom rows can be inserted into the data set by another user and are included in later reads in the current transaction. Because concurrency is lower than the default isolation level, use this option only when necessary.
Serializable Places a range lock on the data set, preventing other users from updating or inserting rows into the data set until the transaction is complete. This is the most restrictive of the four isolation levels. Because concurrency is lower, use this option only when necessary. This option has the same effect as setting HOLDLOCK on all tables in all SELECT statements in a transaction.
Setting Isolation Levels SET TRANSACTION ISOLATION LEVEL { READ COMMITTED | READ UNCOMMITTED | REPEATABLE READ | SERIALIZABLE }
Isolation Levels • Defaults to Read Committed • For almost all programs this will be sufficient
Building Test Tables • Go to my web page and get the file ‘student class.sql’ • Run this script in YOUR database ‘CCUSERXX’ • This will build the Student Class database. We will use the database often going forward
Batch File to Load Data insert into tblLocation (building_id, room_id, room_capacity) values ('HOV', 209, 10); insert into tblLocation (building_id, room_id, room_capacity) values ('HOV', 103, 25); Commit transaction -- build location -- just do a series of inserts, nothing fancy Use ccuserxx Begin transaction insert into tblLocation (building_id, room_id, room_capacity) values ('CRA', 513, 25); insert into tblLocation (building_id, room_id, room_capacity) values ('SCI', 111, 20);
-- build location -- just do a series of inserts, nothing fancy Use ccuserxx Delete from tblLocation Begin transaction insert into tblLocation (building_id, room_id, room_capacity) values ('CRA', 513, 25); insert into tblLocation (building_id, room_id, room_capacity) values ('SCI', 111, 20); insert into tblLocation (building_id, room_id, room_capacity) values ('HOV', 209, 10); -- note we are about try and insert a duplicate record insert into tblLocation (building_id, room_id, room_capacity) values ('HOV', 209, 10); Commit transaction Select * from tblLocation Error Conditions In Batch Files
-- build location -- just do a series of inserts, nothing fancy Use ccuserxx Delete From tblLocation; Begin transaction insert into tblLocation (building_id, room_id, room_capacity) values ('CRA', 513, 25); IF (@@ERROR <> 0) GOTO on_error insert into tblLocation (building_id, room_id, room_capacity) values ('SCI', 111, 20); IF (@@ERROR <> 0) GOTO on_error insert into tblLocation (building_id, room_id, room_capacity) values ('HOV', 209, 10); IF (@@ERROR <> 0) GOTO on_error insert into tblLocation (building_id, room_id, room_capacity) values ('HOV', 209, 10); IF (@@ERROR <> 0) GOTO on_error Commit transaction Goto finished On_error: Rollback transaction Finished: Select * from tblLocation Batch File with Error Handling