Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

SQL Server Transaction Management

7.301 visualizaciones

Publicado el

This slideshow was created by Denise McIerney of Intuit.

It was presented to the San Francisco SQL Server User Group in April 2008.

Publicado en: Tecnología
  • Sé el primero en comentar

SQL Server Transaction Management

  1. 1. SQL Server Transaction Management Denise McInerney San Francisco SQL Server User Group April 9, 2008
  2. 2. <ul><li>Introduction </li></ul><ul><li>What is a transaction </li></ul><ul><li>Auto-commit </li></ul><ul><li>Explicit transactions </li></ul><ul><li>Nested transactions </li></ul><ul><li>Catching errors </li></ul><ul><li>Coding efficient transactions </li></ul><ul><li>Isolation levels </li></ul><ul><li>Conclusion </li></ul>Agenda
  3. 3. Introduction <ul><li>10 years SQL Server DBA </li></ul><ul><ul><li>MCDBA/MCTS </li></ul></ul><ul><li>Web-based OLTP applications </li></ul><ul><li>Focus on performance tuning </li></ul><ul><li>Development DBA www.intuitmarket.com </li></ul><ul><li>PASS volunteer </li></ul><ul><ul><li>Women in Technology SIG Chairperson </li></ul></ul>
  4. 4. <ul><li>Single, logical unit of work </li></ul><ul><ul><li>Sequence of operations </li></ul></ul><ul><ul><li>Dictated by business rules </li></ul></ul><ul><ul><ul><li>Debit/credit </li></ul></ul></ul><ul><ul><ul><li>Order header/order detail </li></ul></ul></ul><ul><li>Why do we care? </li></ul><ul><ul><li>Data integrity </li></ul></ul><ul><ul><li>Performance </li></ul></ul>What is a transaction?
  5. 5. <ul><li>A tomicity </li></ul><ul><ul><li>All or Nothing; everything commits, or everything rolls back </li></ul></ul><ul><li>C onsistency </li></ul><ul><ul><li>All constraints and rules are honored and all data structures are intact, even if system fails </li></ul></ul>ACID Properties
  6. 6. ACID Properties continued <ul><li>I solation </li></ul><ul><ul><li>Concurrent transactions are kept separate, aka “serialized” </li></ul></ul><ul><ul><li>Separate transactions never see data in an intermediate state </li></ul></ul><ul><ul><li>Managed with locking </li></ul></ul><ul><li>D urability </li></ul><ul><ul><li>After commit, effects of transaction persist even if system fails </li></ul></ul>
  7. 7. <ul><li>Default mode for SQL Server </li></ul><ul><li>Every T-SQL command that is successfully completed commits </li></ul><ul><li>If command encounters an error, transaction is rolled back </li></ul><ul><li>Over-ridden with explicit transaction </li></ul><ul><ul><li>BEGIN TRAN </li></ul></ul>Auto-Commit
  8. 8. <ul><li>Defined by the user </li></ul><ul><li>Automatically abort only in case of fatal error </li></ul><ul><li>TSQL </li></ul><ul><ul><li>Begin Tran </li></ul></ul><ul><ul><li>Commit Tran/Commit Work </li></ul></ul><ul><ul><li>Rollback Tran/Rollback Work </li></ul></ul><ul><li>OLEDB </li></ul><ul><ul><li>ITransactionLocal::StartTransaction </li></ul></ul><ul><ul><li>ITransaction::Commit/ITransaction::Abort </li></ul></ul><ul><li>ADO.NET SqlConnection object </li></ul><ul><ul><li>BeginTransaction </li></ul></ul><ul><ul><li>Commit()/Rollback() </li></ul></ul><ul><li>ADO Connection object </li></ul><ul><ul><li>BeginTrans </li></ul></ul><ul><ul><li>CommitTrans/Rollbacktrans </li></ul></ul>Explicit Transactions
  9. 9. <ul><li>TSQL Transaction DEMO </li></ul>
  10. 10. <ul><li>Crucial to managing transactions properly </li></ul><ul><li>In TSQL </li></ul><ul><ul><li>Check @@ERROR </li></ul></ul><ul><ul><ul><li>System error message </li></ul></ul></ul><ul><ul><ul><li>True for SQL command immediately proceeding </li></ul></ul></ul><ul><ul><ul><li>Pass back to calling program and take action </li></ul></ul></ul><ul><ul><ul><li>Take action in SQL code </li></ul></ul></ul><ul><ul><li>Return user-defined error message via RAISERROR </li></ul></ul><ul><ul><ul><li>Store in sysmessages </li></ul></ul></ul><ul><ul><ul><li>Build dynamically </li></ul></ul></ul><ul><ul><ul><li>Can be logged </li></ul></ul></ul><ul><ul><ul><li>Can take parameters </li></ul></ul></ul><ul><ul><li>TRY…CATCH </li></ul></ul><ul><ul><li>SET XACT_ABORT ON </li></ul></ul><ul><ul><ul><li>Provides protection, but silently </li></ul></ul></ul>Errors During Transactions
  11. 11. <ul><li>Error Handling DEMO </li></ul>
  12. 12. <ul><li>Possible to nest transaction blocks </li></ul><ul><li>BUT! </li></ul><ul><ul><li>Committing inner transactions is ignored </li></ul></ul><ul><ul><li>Only outermost COMMIT will have effect </li></ul></ul><ul><ul><li>ROLLBACK will rollback all levels </li></ul></ul><ul><li>@@TRANCOUNT </li></ul><ul><ul><li>Returns nesting level of current transaction block </li></ul></ul><ul><ul><li>Each BEGIN TRAN increments by 1 </li></ul></ul><ul><ul><li>COMMIT decrements by 1 </li></ul></ul><ul><ul><li>ROLLBACK decrements to 0 </li></ul></ul>Nested Transactions
  13. 13. <ul><li>Nested Transaction DEMO </li></ul>
  14. 14. <ul><li>Requires planning </li></ul><ul><li>Keep as short as possible </li></ul><ul><li>Access the least amount of data possible </li></ul><ul><li>Do not wait for user input during a transaction </li></ul><ul><li>Make intelligent use of isolation levels </li></ul>Coding Efficient Transactions
  15. 15. Isolation Levels <ul><li>Read Uncommitted </li></ul><ul><ul><li>aka (NOLOCK) </li></ul></ul><ul><ul><li>Does not issue shared lock, does not honor exclusive lock </li></ul></ul><ul><ul><li>Rows can be updated/inserted/deleted before transaction ends </li></ul></ul><ul><ul><li>Least restrictive </li></ul></ul><ul><li>Read Committed </li></ul><ul><ul><li>SQL Server default </li></ul></ul><ul><ul><li>Holds shared lock </li></ul></ul><ul><ul><li>Cannot read uncommitted data, but data can be changed before end of transaction, resulting in nonrepeatable read or phantom rows </li></ul></ul>
  16. 16. Isolation Levels continued <ul><li>Repeatable Read </li></ul><ul><ul><li>Locks data being read, prevents updates/deletes </li></ul></ul><ul><ul><li>New rows can be inserted during transaction, will be included in later reads </li></ul></ul><ul><li>Serializable </li></ul><ul><ul><li>aka HOLDLOCK on all tables in SELECT </li></ul></ul><ul><ul><li>Locks range of data being read, no modifications are possible </li></ul></ul><ul><ul><li>Prevents updates/deletes/inserts </li></ul></ul><ul><ul><li>Most restrictive </li></ul></ul>
  17. 17. <ul><li>Isolation Level DEMO </li></ul>
  18. 18. Isolation Levels Summary From Books Online “Managing Concurrent Data Access” No No No Serializable Yes No No Repeatable Read Yes Yes No Read committed Yes Yes Yes Read Uncommitted Phantom Rows Non-Repeatable Read Dirty Read Isolation Level
  19. 19. Isolation Levels Added in SS2005 <ul><li>READ_COMMITTED_SNAPSHOT </li></ul><ul><ul><li>New implementation of READ COMMITTED </li></ul></ul><ul><ul><li>Uses row versioning instead of locking </li></ul></ul><ul><ul><li>ALTER DATABASE option </li></ul></ul><ul><ul><li>When turned ON, transactions setting the read committed isolation level use row versioning </li></ul></ul><ul><li>SNAPSHOT </li></ul><ul><ul><li>Uses row versioning instead of locking </li></ul></ul><ul><ul><li>Only recognize data modifications that were committed before the start of the transaction </li></ul></ul><ul><ul><li>Data modifications made by others after start of transaction are ignored </li></ul></ul><ul><li>Both </li></ul><ul><ul><li>Use TEMPDB heavily </li></ul></ul><ul><ul><li>Minimize contention </li></ul></ul><ul><ul><li>Provide read consistency </li></ul></ul>
  20. 20. <ul><li>Snapshot Isolation DEMO </li></ul>
  21. 21. Snapshot Isolation Summary From Books Online “Row Versioning-based Isolation Levels in the Database Engine” Uses row versions to select rows to update. Tries to acquire an exclusive lock on the actual data row to be modified, and if the data has been modified by another transaction, an update conflict occurs and the snapshot transaction is terminated. Reverts from row versions to actual data to select rows to update and uses update locks on the data rows selected. Acquires exclusive locks on actual data rows to be modified. No update conflict detection. How updates are handled. Integrated support. Cannot be disabled. None. Update conflict detection. All data that was committed before the start of each transaction. All data that was committed before the start of each statement. The version of data read by statements. Requires the execution of SET TRANSACTION ISOLATION LEVEL to specify the SNAPSHOT isolation level before the start of the transaction. Use the default read-committed isolation level, or run the SET TRANSACTION ISOLATION LEVEL statement to specify the READ COMMITTED isolation level. This can be done after the transaction starts. How a session requests the specific type of row versioning. ALLOW_SNAPSHOT_ISOLATION READ_COMMITTED_SNAPSHOT The database option that must be set to ON to enable the required support. Snapshot isolation level Read-committed snapshot isolation level Property
  22. 22. Conclusion <ul><li>Transaction management crucial </li></ul><ul><ul><li>Guaranteeing data integrity </li></ul></ul><ul><ul><li>Performance </li></ul></ul><ul><li>Understand how transactions work </li></ul><ul><li>Understand what’s acceptable for your system </li></ul><ul><ul><li>Choose appropriate isolation level </li></ul></ul><ul><li>Assume nothing! </li></ul><ul><li>DBA & developer collaboration key </li></ul><ul><li>Email: denise.mcinerney@sqlpass.org </li></ul>

×