DISTRIBUTED TRANSACTIONS IN .NET - PowerPoint PPT Presentation

About This Presentation
Title:

DISTRIBUTED TRANSACTIONS IN .NET

Description:

catch (Exception ex) //rollback all the database operations if exceptions occur ... of the Department() and Employee() classes. Department dept = new Department ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 19
Provided by: Div59
Learn more at: https://www.cs.odu.edu
Category:

less

Transcript and Presenter's Notes

Title: DISTRIBUTED TRANSACTIONS IN .NET


1
DISTRIBUTED TRANSACTIONS IN .NET
  • Presented By Divya Josyala.

2
Contents
  • 1.Overview
  • - Transaction
  • - Local Transaction
  • - Distributed Transaction
  • - Acid Properties
  • 2. Distributed transactions in .Net
  • - Windows Application
  • System.Transactions namespace -
    Demo
  • - Web Services
  • System.EnterpriseService
    namespaces - Demo
  • - File Transaction Demo
  • 3. References

3
Overview
  • What is a transaction?
  • A transaction is a unit of work .
  • Example bank transfer operation.
  • The application transfers funds from one
    --account to another by debiting money from one
    account and crediting it to another.

4
Overview
  • Local Transaction transactions are performed on
    a single database

Transfer()
Charge()
Credit()
database
5
Overview
  • Distributed Transaction
  • - Transactions that span two or more
    databases.
  • - Incorporates several distinct operations
    occurring on different systems into an atomic
    action that succeeds or fails completely.

Transfer()
Credit()
Charge()
Database1
Database2
6
Overview ACID Properties
  • ACID properties of a transaction
  • Atomicity
  • Either all the operations in a transaction
    should complete or none of them should.
  • Consistency
  • A transaction should preserve the
    consistency of data
  • Isolation
  • Requires that each transaction appears to
    be the only transaction manipulating the data
    source irrespective of other transactions
    running concurrently.
  • Durability
  • If a transaction succeeds the system should
    guarantee that its updates will persist , even if
    the computer crashes immediately after the
    application performs a commit operation

7
Distributed Transactions in .Net
  • .Net framework provides support for
    distributed transactions
  • in two ways
  • TransactionScope class in System.Transactions
    namespace
  • - Used in windows and web applications.
  • ContextUtil class in System.EnterpriseServices
    namespace
  • - Used in web services.

8
Distributed Transactions in .Net
  • Method 1
  • System. Transactions namespace defines the
    TransactionScope class which enables you to
    create and manage distributed transactions.
  • A Transaction scope defines a block of code that
    participates in a transaction.
  • The first connection to a database within the
    transaction scope
  • enlists it as a local transaction
  • Subsequent connections to databases within the
    transaction scope promotes the local transaction
    to a distributed transaction.

9
TransactionOptions
  • // Create the TransactionOptions object
  • TransactionOptions TranOpt new
    TransactionOptions()
  • // Set the Isolation Level
  • TranOpt.IsolationLevel
  • System.Transactions.IsolationLevel.ReadCommitt
    ed
  • // Set the timeout to be 2 minutes
  • // Uses the (hours, minutes, seconds)
    constructor
  • TimeSpan Time new TimeSpan(0, 2, 0)
  • TranOpt.Timeout Time

10
TransactionScopeOptions
TransactionScopeOptions Description
Required If within a currently active transaction scope, this transaction scope will join it. Otherwise it will create its own transaction scope.
RequiresNew This transaction will create its own transaction scope.
Supports If within a currently active transaction scope, this transaction scope will join it. Otherwise no transaction scope will be created.
NotSupported No transaction scope will be created.
11
Transaction Scope - Code
  • //Create a transaction scope object
  • using (TransactionScope oTranScope new
    TransactionScope())
  • using (SqlConnection oCn1 new
    SqlConnection(this.sCn1))
  • SqlCommand oCmd new
    SqlCommand(this.sSQL, oCn1)
  • oCn1.Open()
  • oCmd.ExecuteNonQuery()
  • oCn1.Close()
  • // Tells the transaction scope
    that the transaction is in a
  • // consistent state and can be
    committed
  • oTranScope.Complete()
  • // The following bracket
    completes, commits, and disposes
  • // the transaction

12
Distributed Transactions in .Net
  • METHOD 2
  • Systems.EnterpriseServices namespace defines
    the
  • contextutil class .
  • Methods in the contextutil class used for
    distributed transactions are
  • 1.SetComplete() Indicates that the current
    transaction is committed and the object
    deactivated
  • 2.SetAbort() Indicates that the current
    transaction has to be rolled back and the object
    deactivated.
  • AutoComplete attribute calls the
    SetComplete method if no exceptions occur or
    SetAbort method if an exception is thrown.
  • WebMethod(false, TransactionOption.Required)
  • AutoComplete

13
WebService - DEPT
  • WebMethod(false, TransactionOption.Required)
  • public int AddDept(string deptName,
    string location)
  • try
  • string connString
    System.Configuration.ConfigurationManager.Connect
    ionStrings "ConnectionString".ConnectionString
  • int deptNo
  • //Create the connection object
    passing to it the connection string
  • SqlConnection connection new
    SqlConnection(connString)
  • connection.Open()
  • //Create and set the SqlCommand
    object and pass it the name of the
  • // stored procedure to be executed
  • SqlCommand command new
    SqlCommand("AddDept", connection)
  • //Indicates that you want to
    execute a stored procedure
  • command.CommandType
    CommandType.StoredProcedure

14
WebService - DEPT
  • //Add the DeptName parameter
  • SqlParameter paramDeptName new
  • SqlParameter("_at_DeptName",
    SqlDbType.VarChar, 50)
  • paramDeptName.Value deptName
  • paramDeptName.Direction
    ParameterDirection.Input
  • command.Parameters.Add(paramDeptNa
    me)
  • //Add the Location parameter
  • SqlParameter paramLocation new
  • SqlParameter("_at_Location",
    SqlDbType.VarChar, 50)
  • paramLocation.Value location
  • paramLocation.Direction
    ParameterDirection.Input
  • command.Parameters.Add(paramLocati
    on)
  • //Add the DeptNo parameter as the
    Output parameter
  • SqlParameter paramDeptNo new
  • SqlParameter("_at_DeptNo",
    SqlDbType.Int, 4)
  • paramDeptNo.Direction
    ParameterDirection.Output
  • command.Parameters.Add(paramDeptNo
    )

15
WebService - DEPT
  • // Execute the Command
  • command.ExecuteNonQuery()
  • //you can retrieve the output
    parameter from the parameters collection of
  • //the SQL Command object
  • deptNo (int)command.Parameters"
    _at_DeptNo".Value
  • //commiting the transaction using
    the SetComplete method of the
  • //ContextUtil Class
  • ContextUtil.SetComplete()
  • return deptNo
  • catch (Exception ex)
  • //rollback all the database
    operations if exceptions occur
  • ContextUtil.SetAbort()
  • throw ex

16
Calling Webservice - CODE
  • WebMethod(false, TransactionOption.Required)
  • public bool AddDeptEmployees(string
    deptName, string deptLocation,
  • string empName, string empAddres)
  • try
  • int deptNo
  • //Create instances of the
    Department() and Employee() classes
  • Department dept new
    Department()
  • Employee emp new Employee()
  • //Add the Dept details to the
    Dept table
  • deptNo dept.AddDept(deptName,
    deptLocation)
  • //Add the Employee details to the
    Emp table
  • //int empNo emp.AddEmp(empName,e
    mpAddres,deptNo)
  • int empNo emp.AddEmp(empName,
    empAddres, 200)
  • return true

17
REFERENCES
  • MSDN - DATA POINTS By John Papa
  • Url http//msdn.microsoft.com/msdnmag/issues/05/0
    2/DataPoints/
  • Develop Transactional .Net Web Services By
    Thiru Thangarathinam
  • Url http//www.developer.com/net/asp/article.php/
    3385631

18
Comments / Questions ??
  • THE END
Write a Comment
User Comments (0)
About PowerShow.com