320 likes | 478 Views
Distributed Transaction & Long-running transactions. Krasimir Parushev Consultant, ITCE ltd. Rossen Zhivkov Freelance SharePoint Consultant. January 19 th , 2008 Sofia, Bulgaria. What if…. What if you have to update a SQL Server database and Oracle database in one transaction?
E N D
Distributed Transaction & Long-running transactions Krasimir ParushevConsultant, ITCE ltd. Rossen ZhivkovFreelance SharePoint Consultant January 19th, 2008Sofia, Bulgaria
What if… • What if you have to update a SQL Server database and Oracle database in one transaction? • What if you have to call two web services in one transaction? • What if you have to implement transaction that lasts for days?
What is… • What is distributed transaction • What is 2-phase commit protocol • What is transaction coordinator • What is resource manager • What is long-running transaction • WS-Coordination, WS-AtomicTransaction, WS-Business Activity • What is BPEL4WS
Roles in Transaction System • Application • Begins the transaction • Makes changes to resources • Can Commit or Abort the transaction • Transaction Manager • Transaction demarcation, participants, and state • Prepare result is made durable • Can Abort the transaction • Resource Managers • Controls changes to resources under the scope of a transaction • Uses a (durable) log for recovery • Can Abort the transaction • Need to participate in 2PC (Atomicity) • Provide Isolation of its resources • Provide Durability of its resources
VB Object VB Object ADO Conn VB Object ADO Conn ADO Conn Local Transactions Application DBMS Activity Lock Manager Transaction Manager Client 1 Activity Client 2 Data Activity Client N
Local Transactions • All updates within a single DBMS • Or other “resource manager” (RM) • DBMS provides transaction demarcation • Begin (start), end (commit), abort (rollback) • DBMS provides transaction manager • DBMS provide lock manager
Computer 2 RM2 -SQL Server Computer 1 RM1 - MSMQ Participating DTC Computer 3 The Coordinating DTC RM3 - ORACLE Participating DTC A Distributed Transaction Data Your Tx COM+ Application RM Proxy Queue RM Proxy RM Proxy Data
Distributed Transactions • Transactions may span multiple RMs • Databases, Queues, etc. • Distributed Transaction Manager • Provides common transaction demarcation • Coordinates commit/abort across RMs • 2 Phase Commit (2PC) Protocol • RMs are “prepared” to either commit or abort • RMs are notified of final outcome • RM recovery • Resource Managers • Need to participate in 2PC (Atomicity) • Provide Isolation of its resources • Provide Durability of its resources
Transaction Manager Dispenses new transactions to clients Tracks the resource managers participating in the transaction Coordinates the transaction outcome Resource Manager A subsystem that allows updates to transaction protected data Transaction Manager File System SQL Server SQL Server Resource Managers Resource Manager Two-Phase Commit
Two-Phase Commit Resource Manager Transaction Manager Prepare Prepare and Force write “Prepared” record to RM log Prepared Force write “Commit” record to TM log Commit Lazy write “Committed” record to RM log and Release locks Committed Lazy write “Committed” record to TM log
Two Phase Commit – Failure Resource Manager Transaction Manager Abort on Failure Prepare Abort on Failure Prepare & Write “Prepared” record to RM log Prepared Will Abort Write “Commit” record to TM log Indoubt on Failure Commit Will Commit Write “Committed” record to RM log & Release locks Commit Committed Commit Write “Committed” record to TM log
MS-DTCDistributed Transaction Coordinator • Windows NT Service • Two Phase Commit (2PC) and Recovery • Infrastructure • COM Interfaces (a.k.a., DTC proxy) • Used for App TM, TM RM communication • Local TM, remote TM, TM failover • Supported Resource Managers (RM) • SQL Server, MSMQ, BizTalk, COM+ CRM, … • Oracle, DB2, …
SQL Server And Dist Tx • Client initiated distributed transaction • Existing DTC transaction is passed to SQL Server using data access API • ODBC • SQL_ATTR_ENLIST_IN_DTC connection attribute • OLE-DB • ITransactionJoin::JoinTransaction • ADO • Only implicit via MTS/COM+ • System.Data.SqlClient • v1.1 SqlConnection.EnlistDistributedTransaction • System.Transactions • Later… • DB-Library – dbenlisttrans • COM+, System.EnterpriseServices, WCF
Application NT NT MS DTC MS DTC SQL Server SQL Server SQL Server And Dist. Tx… • Server initiated • BEGIN DISTRIBUTED TRANSACTION • Linked Server call • Extended Stored Procedures • X-Transactions • Partitioned Tables
Using distributed transactions in .NET System.Transactions • System.Transactions = API for programming local & distributed transactions • Enlisting resources in distributed transactions • Ambient Transactions • Transaction Promotion • Transaction Flow
Custom Resource Managers • Why to write own resource managers • Incorporate custom resources in distributed transactions • Implement logic for two-phase commit steps (Prepare, Commit, Rollback) • Volatile resource managers • Transactional variables • Transactional collections • Transactional files…. • Sample
Transactional NTFS & Registry • Kernel Transaction Memory • Kernel objects in transaction (distributed / local) • Allows atomic operations for: • Many operations on single file • Operations for multiple files • Operations for many computers • Registry modifications • Sample
Windows Communication Foundation • What is WCF? • Web services + WSE + WS-specifications + MSMQ + EnterpriseServices(COM+) + Remoting • Reliable messaging • Securable services • Protocols support • New abstraction level • Data Abstraction • Communication Abstraction
WCF and Transactions • WCF services & other resource managers can be executed within the scope of distributed transaction • Transactable Services • In the Internet – using WS-AtomicTransaction • In the Intranet - DTC, OleTransaction protocol, System.Transaction • MSDTC and WS-AtomicTransactions • Transaction Flow
Incorporate web services in distributed transactions using WCF
Long-running transactions • The business scenario • We ”cannot” lock the service sessions (and the underlying database) for that long time • Are ACID properties maintained? • Is it OK to hold locks for days • Compensation actions • Logic to “undo” the effects from previously committed action
WS-* specifications • WS-Coordination • Distribution of the transaction context • WS-AtomicTransaction • Pure ACID for short transactions with 2PC or similar • Locking resources • WS-Business Activity • Long living transactions • To coordinate large work implemented by several small WS-AtomicTransaction services • Compensations • SOAP “compensate” message
Business Process Execution Language For Web Services - BPEL4WS • What is BPEL4WS: • Orchestrating web services to implement business process • Complex transactions modeled as … WORKFLOW • Provides sequences, branches, iterations, etc • Bound to local scope • Use WS-BusinessActivity to incorporate distributed scopes
Comparison: Distributed vs. Long-running • Transaction trees • In distributed transactions: • Parent NEVER commits before the child commits (2PC) • Parent / Child hold locks till end of transaction • In long-running transactions: • Parent can commit independently of the child transaction • If Child then rollbacks, parent executes compensation logic • Parent / Child release locks immediately after commit
Workflow Foundation • Business Processes orchestration • Support for Distributed transactions via TransactionScope • Support for Long-running transactions via Compensations • Persistence and Tracking • Integration with WCF & Web Services • Published as WCF/web service • Utilizes WCF/Web service
Implement long-running transactions in Windows Workflow Foundation
Summary • Distributed Transactions • 2-Phase Commit Protocol • SQL Server 2005 & Distributed transactions • .NET & Distributed Transactions • Custom Resource Managers • Distributed Transactions in Web Services • Windows Communications Foundation & WS-AtomicTransaction • Long-running transactions • Workflow Foundation • BPEL4WS & WS-BusinessActivity • Next steps
Thank You for you time Rossen Zhivkovrzhivkov@gmail.com Krasimir Parushevkrasimir@itce.com