Sie sind auf Seite 1von 33

Transactions 2.

Jörg Neumann
Jörg Neumann
 Principal Consultant bei Acando
 Associate bei Thinktecture
 MVP im Bereich „Client App Dev“
 Beratung, Schulung, Coaching
 Buchautor, Speaker
 Mail
– Joerg.Neumann@Acando.de
– Joerg.Neumann@thinktecture.com
 Blog
– www.HeadWriteLine.BlogSpot.com
Agenda
 Ein paar Hintergründe
 Welche Probleme löst System.Transactions?
 Programmiermodelle
 Resource Manager implementieren
Was ist eine Transaktion?
 Eine Transaktion ist ein Sammlung von
Operationen, die im Rahmen eines
(Geschäfts-)Prozesses durchgeführt werden
 Ziel ist die Wahrung der Konsistenz im
Fehlerfall
ACID
 Atomic
– Alle Aktionen verhalten sich wie eine Operation
 Consistent
– Konsistenter Zustand nach Beendigung
 Isolated
– Die Aktionen beeinflussen sich nicht gegenseitig
 Durable
– Das Ergebnis wird dauerhaft gespeichert
Beteiligte Parteien
 Resource Manager (RM)
– Systeme die Transaktionen unterstützten
 Transaction Manager (TM)
– Verwalten von Transaktionen
– Resource Manager „enlisten“ sich im TM
Verteilte Transaktionen
 Mehrere Resource Manager sind beteiligt
 App Domain-Grenze wird überschritten
 MSDTC (Distributed Transaction Coordinator)
 Two-Phase Commit Protokoll
Two-Phase Commit
 Gewährleistung der „Atomarität“ bei
verteilten Transaktionen
– Phase 1: Operation vorbereiten (Prepare)
– Phase 2: Commit/Rollback durchführen
 Probleme
– Locking
– Performance
Client
DTC
OK

Prepare Prepare
Commit Commit
OK OK

DTC DTC
Datenbank A Datenbank B
Lokale Transaktionen
 App Domain-Grenze wird nicht überschritten
 Durable & volatile Resources
 Gute Performance
 Kein DTC notwendig
Agenda
 Ein paar Hintergründe
 Welche Probleme löst System.Transactions?
 Programmiermodelle
 Resource Manager implementieren
Programmiermodelle (.NET 1.x)
 ADO.NET
– IDbTransaction
– SqlTransaction, ...
 Enterprise Services
– ServicedComponent
 MSMQ, T-SQL, ...
ADO.NET

Dim connection As New SqlConnection(connectionString)


connection.Open()
Dim command As New SqlCommand(sql, connection)
Dim transaction As SqlTransaction = connection.BeginTransaction()
command.Transaction = transaction
Try
' Zugriffscode…
transaction.Commit()
Catch
transaction.Rollback()
Finally
connection.Close()
End Try
Enterprise Services

<System.ComponentModel.Transaction()> _
Public Class MyComponent
Inherits System.ComponentModel.ServicedComponent

<AutoComplete()> _
Public Sub MyMethod()
' Logik…
End Sub
End Class
Einschränkungen
 ADO.NET-Transaktionen
– Nur eine Ressource (DB) pro Transaktion
 Enterprise Services
– Ableitung von ServicedComponent erforderlich
– COM+-Registrierung erforderlich
(mit „Services without Components“ geht‘s ohne)
– DTC wird immer verwendet
Programmiermodelle (.NET 2.0)
 System.Transactions
– Ein System für alles!
 Lokale und verteilte Transaktionen
 Durable & volatile Resource Manager
 Einheitliches Programmiermodell
– Keine Ableitung notwendig
– Keine COM+-Registrierung nötig
– Arbeitet mit ADO.NET & ES zusammen
System.Transactions
 Enthält Lightweight Transaction Manager
– In-Memory TX-Manager für
Single-Phase Commit RMs
 Bessere Performance bei SPC-Transaktionen
 Beschränkt auf eine App Domain
 Eigene RMs können leicht erstellt werden
 Bietet Promotion
Implementierung
 Explizit
– Transaction
– CommitableTransaction
 Implizit
– TransactionScope
 Deklarativ
– Transaction-/AutoComplete-Attribute
Klassenmodell

Transaction

Current EnlistPromotableSinglePhase Clone


IsolationLevel EnlistVolatile DependentClone
TransactionInform. EnlistDurable Rollback

CommitableTransaction DependentTransaction
BeginCommit Complete
Commit
EndCommit
Ambient Transaction
 Für implizites Enlistment/Commit/Rollback
über TransactionScope
 Automatisches Enlistment durch RMs
 Zentraler Zugriff über
Transaction.Current
Funktionsweise

Using scope As New TransactionScope()

Dim con As new SqlConnection(conString)


con.Open()
...
scope.Complete()
End Using

If
Transaction.Current
Me.complete
Not Transaction.Current
= True = new CommittableTransaction()
Is Nothing Then
Transaction.Current
Transaction.Current.Rollback();
Transaction.Current.EnlistPromotableSinglePhase(Me)
= Nothing
End If
Optionen
 TransactionOptions
– IsolationLevel
– Timeout
 TransactionScopeOption
– Required Unterfunktion enlisted sich in aktueller TX

– RequiresNew Für Unterfunktion wird eine neue TX erstellt

– Suppress Unterfunktion nimmt nicht an TX teil


Threading
 Mehrere Threads nehmen an einer
Transaktion teil
 DependentTransaction
– Transaction.DependentClone()
– Garantiert, dass Transaktion nicht beendet wird
bevor alle Aktionen abgeschlossen wurden
Promotion
 Automatisches Hochstufen einer lokalen zu
einer verteilten Transaktion
 TX startet lokal (Inprocess)
 TX läuft weiter lokal, solange nur lokale RMs
verwendet werden (eine App Domain)
 Sonst: Promotion zu DTC-Transaktion
PSPE
 Promotable Single Phase Enlistment (PSPE)
 Zur Zeit nur in SQL Server 2005 / 2008
 TX startet lokal (Inprocess)
 LTM übergibt Kontrolle an PSPE-RM
 Kein DTC nötig
 Kommen weitere RMs hinzu findet Promotion
statt
Enlist

Promotion
SQL Server 2005
LTM DTC

Enlist DTC
SQL Server 2005
DTC
Enlistment von SqlConnection
 Explizites Modell
– SqlConnection.EnlistTransaction()
 Implizites Modell
– TransactionScope
– Connection enlisted sich automatisch
 Connection String
– Enlist=true
– Transaction Binding=Implicit Unbind
Eigene RMs implementieren
 Klasse muss IEnlistmentNotification
implementieren

Public Interface IEnlistmentNotification


Sub Prepare(preparingEnlistment As PreparingEnlistment)
Sub Commit(enlistment As Enlistment)
Sub Rollback(enlistment As Enlistment)
Sub InDoubt(enlistment As Enlistment)
End Interface
Sonstiges
 Vista
– Transaktionales Dateisystem
– Transaktionale Registry
– Zur Zeit nur über unmanaged API verfügbar
 WCF
– Verwendet System.Transactions
– WS-Atomic Transaction Support im DTC
Zusammenfassung
 Leichter Umgang mit Transaktionen
 Einheitliches Programmiermodell
 Support für volatile Ressourcen
 Effizienter Umgang mit Single-Face-RMs
 Automatisches Herauf stufen der TX
 Eigene RMs können leicht erstellt werden
Weiterführende Artikel
 Whitepaper: Introducing System.Transactions
http://www.microsoft.com/downloads/details.aspx?familyid=aac3d722-
444c-4e27-8b2e-c6157ed16b15&displaylang=en
 Transaction Management in Windows
http://www.microsoft.com/windowsserver2003/appserver/transmgmt.
mspx
 Transactions Programming Forum
http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=388&Si
teID=1
 System.Transactions-Samples von Juval Loewy
http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=5&tabid
=11#Essentials
 Jim Johnson's Blog
http://pluralsight.com/blogs/jimjohn
 Nate Moch's Blog
http://blogs.msdn.com/natemoch
 Florin Lazar's Blog
http://blogs.msdn.com/florinlazar/
{ In-depth support and consulting for
software architects and developers }
http://www.thinktecture.com/

joerg.neumann@thinktecture.com
www.HeadWriteLine.BlogSpot.com

33

Das könnte Ihnen auch gefallen