distributed transaction & long-running transactions

32
Distributed Transaction & Long-running transactions Rossen Zhivkov Freelance SharePoint Consultant January 19 th , 2008 Sofia, Bulgaria Krasimir Parushev Consultant, ITCE ltd.

Upload: halden

Post on 15-Jan-2016

43 views

Category:

Documents


0 download

DESCRIPTION

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? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Distributed Transaction  &  Long-running transactions

Distributed Transaction &

Long-running transactions

Rossen ZhivkovFreelance SharePoint Consultant

January 19th, 2008Sofia, Bulgaria

Krasimir ParushevConsultant, ITCE ltd.

Page 2: Distributed Transaction  &  Long-running transactions

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?

Page 3: Distributed Transaction  &  Long-running transactions

What is…

What is distributed transactionWhat is 2-phase commit protocolWhat is transaction coordinatorWhat is resource manager

What is long-running transactionWS-Coordination, WS-AtomicTransaction, WS-Business ActivityWhat is BPEL4WS

Page 4: Distributed Transaction  &  Long-running transactions

Roles in Transaction System

ApplicationBegins the transactionMakes changes to resourcesCan Commit or Abort the transaction

Transaction ManagerTransaction demarcation, participants, and statePrepare result is made durableCan Abort the transaction

Resource ManagersControls changes to resources under the scope of a transactionUses a (durable) log for recoveryCan Abort the transactionNeed to participate in 2PC (Atomicity)Provide Isolation of its resourcesProvide Durability of its resources

Page 5: Distributed Transaction  &  Long-running transactions

Local Transactions

DBMSDBMS ApplicationApplication

ActivityActivity

ActivityActivity

ActivityActivity

ADOADOConnConn

Client 1Client 1

Client 2Client 2

Client NClient N

Transaction Transaction ManagerManager

LockLockManagerManager

DataData

VB VB ObjectObject

ADOADOConnConn

VB VB ObjectObject

ADOADOConnConn

VB VB ObjectObject

Page 6: Distributed Transaction  &  Long-running transactions

Local Transactions

All updates within a single DBMSOr other “resource manager” (RM)

DBMS provides transaction demarcationBegin (start), end (commit), abort (rollback)

DBMS provides transaction managerDBMS provide lock manager

Page 7: Distributed Transaction  &  Long-running transactions

Computer 3Computer 3

RM3 - ORACLERM3 - ORACLE

Participating DTCParticipating DTC

Computer 2Computer 2

RM2 -SQL ServerRM2 -SQL Server

Participating DTCParticipating DTC

Computer 1Computer 1

The Coordinating DTC The Coordinating DTC

RM1 - MSMQRM1 - MSMQ

A Distributed Transaction

DataData

DataData

Your Tx COM+ Application Your Tx COM+ Application

QueueQueue

RM ProxyRM Proxy

RM ProxyRM ProxyRM ProxyRM Proxy

Page 8: Distributed Transaction  &  Long-running transactions

Distributed Transactions

Transactions may span multiple RMsDatabases, Queues, etc.

Distributed Transaction ManagerProvides common transaction demarcationCoordinates commit/abort across RMs

2 Phase Commit (2PC) ProtocolRMs are “prepared” to either commit or abortRMs are notified of final outcome

RM recovery

Resource ManagersNeed to participate in 2PC (Atomicity)Provide Isolation of its resourcesProvide Durability of its resources

Page 9: Distributed Transaction  &  Long-running transactions

Two-Phase Commit

Transaction ManagerDispenses new transactions to clientsTracks the resource managers participating in the transactionCoordinates the transaction outcome

Resource ManagerA subsystem that allows updates to transaction protected data

SQLSQLServerServer

TransactionManager

SQLSQLServerServer

FileFileSystemSystem

ResourceManagers

Resource Manager

Page 10: Distributed Transaction  &  Long-running transactions

Two-Phase Commit

TransactionManager

ResourceManager

Prepare

Commit

Prepare and Force write “Prepared” record to RM log

Prepared

Force write “Commit” record to TM log

Lazy write “Committed” record to RM log and Release locks

CommittedLazy write “Committed” record to TM log

Page 11: Distributed Transaction  &  Long-running transactions

Two Phase Commit – Failure

TransactionManager

ResourceManager

Prepare

Commit

Prepare & Write “Prepared” record to RM log

Prepared

Write “Commit” record to TM log

Write “Committed” record to RM log & Release locks

Committed

Write “Committed” record to TM log

Abo

rt o

n F

ailu

reC

omm

it

Abo

rt o

nF

ailu

re

Indo

ubt

on F

ailu

reC

omm

it

Will

Abo

rtW

illC

omm

it

Page 12: Distributed Transaction  &  Long-running transactions

LIVE DEMO

Page 13: Distributed Transaction  &  Long-running transactions

MS-DTCDistributed Transaction Coordinator

Windows NT ServiceTwo Phase Commit (2PC) and Recovery

InfrastructureCOM 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, …

Page 14: Distributed Transaction  &  Long-running transactions

SQL Server And Dist Tx

Client initiated distributed transactionExisting 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.SqlClientv1.1 SqlConnection.EnlistDistributedTransaction

System.TransactionsLater…

DB-Library – dbenlisttrans

COM+, System.EnterpriseServices, WCF

Page 15: Distributed Transaction  &  Long-running transactions

SQL Server And Dist. Tx…

Server initiatedBEGIN DISTRIBUTED TRANSACTIONLinked Server callExtended Stored ProceduresX-TransactionsPartitioned Tables

NT NT

SQLSQLServerServer

SQLSQLServerServer

MS DTC

ApplicationApplication

MS DTC

Page 16: Distributed Transaction  &  Long-running transactions

Using distributed transactions in .NET System.Transactions

System.Transactions = API for programming local & distributed transactions

Enlisting resources in distributed transactionsAmbient TransactionsTransaction Promotion

Transaction Flow

Page 17: Distributed Transaction  &  Long-running transactions

Transaction Flow

scope 4

{Suppress}scope 3{RequiresNew}

Transaction A

Transaction B

scope 1

{Required}

Code block withno ambienttransaction----- -- - - -- -- ---- –- – - - ----- -- –-- –- - –-- ------- -- - - –- –- –

scope 2

{Required}

Page 18: Distributed Transaction  &  Long-running transactions

Transactions from .NET application

Page 19: Distributed Transaction  &  Long-running transactions

Custom Resource Managers

Why to write own resource managersIncorporate custom resources in distributed transactionsImplement logic for two-phase commit steps (Prepare, Commit, Rollback)

Volatile resource managersTransactional variablesTransactional collectionsTransactional files….

Sample

Page 20: Distributed Transaction  &  Long-running transactions

Transactional NTFS & Registry

Kernel Transaction MemoryKernel objects in transaction (distributed / local)Allows atomic operations for:Many operations on single fileOperations for multiple filesOperations for many computersRegistry modifications

Sample

Page 21: Distributed Transaction  &  Long-running transactions

Windows Communication Foundation

What is WCF?Web services + WSE + WS-specifications + MSMQ + EnterpriseServices(COM+) + RemotingReliable messagingSecurable servicesProtocols support

New abstraction levelData AbstractionCommunication Abstraction

Page 22: Distributed Transaction  &  Long-running transactions

WCF and Transactions

WCF services & other resource managers can be executed within the scope of distributed transactionTransactable Services

In the Internet – using WS-AtomicTransactionIn the Intranet - DTC, OleTransaction protocol, System.Transaction

MSDTC and WS-AtomicTransactionsTransaction Flow

Page 23: Distributed Transaction  &  Long-running transactions

Incorporate web services in distributed transactions using WCF

Page 24: Distributed Transaction  &  Long-running transactions

Long-running transactions

The business scenarioWe ”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 actionsLogic to “undo” the effects from previously committed action

Page 25: Distributed Transaction  &  Long-running transactions

LIVE DEMO : Long-running transactions

Page 26: Distributed Transaction  &  Long-running transactions

WS-* specifications

WS-CoordinationDistribution of the transaction context

WS-AtomicTransactionPure ACID for short transactions with 2PC or similarLocking resources

WS-Business ActivityLong living transactions

To coordinate large work implemented by several small WS-AtomicTransaction services

CompensationsSOAP “compensate” message

Page 27: Distributed Transaction  &  Long-running transactions

Business Process Execution Language For Web Services - BPEL4WS

What is BPEL4WS:Orchestrating web services to implement business processComplex transactions modeled as … WORKFLOW

Provides sequences, branches, iterations, etcBound to local scope

Use WS-BusinessActivity to incorporate distributed scopes

Page 28: Distributed Transaction  &  Long-running transactions

Comparison: Distributed vs. Long-running

Transaction treesIn 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 transactionIf Child then rollbacks, parent executes compensation logicParent / Child release locks immediately after commit

Page 29: Distributed Transaction  &  Long-running transactions

Workflow Foundation

Business Processes orchestrationSupport for Distributed transactions via TransactionScopeSupport for Long-running transactions via Compensations

Persistence and TrackingIntegration with WCF & Web Services

Published as WCF/web serviceUtilizes WCF/Web service

Page 30: Distributed Transaction  &  Long-running transactions

Implement long-running transactions in Windows Workflow Foundation

Page 31: Distributed Transaction  &  Long-running transactions

Summary

Distributed Transactions2-Phase Commit ProtocolSQL Server 2005 & Distributed transactions.NET & Distributed TransactionsCustom Resource Managers

Distributed Transactions in Web ServicesWindows Communications Foundation & WS-AtomicTransaction

Long-running transactionsWorkflow Foundation BPEL4WS & WS-BusinessActivity

Next steps

Page 32: Distributed Transaction  &  Long-running transactions

Thank You for you time

Rossen [email protected]

Krasimir [email protected]