Post on 26-Jun-2015
transcript
Slide 1 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
In this session, you will learn to:Manage local transactions
Manage distributed transactions
Objectives
Slide 2 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
A transaction can be defined as a sequence of operations that are performed together as a single logical unit of work.
If a transaction is successful, all the data modifications performed in the database will be committed and saved.
If a transaction fails or an error occurs, then the transaction is rolled back to undo the data modifications done in the database.
Managing Local Transactions
Slide 3 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
For a transaction to commit successfully within a database, it should possess the following four properties:
Atomicity
Consistency
Isolation
Durability
Properties of a Transaction
States that either all the modifications are performed or none of them are performed
States that data is in a consistent state after a transaction is completed successfully to maintain the integrity of data
States that any data modification made by one transaction must be isolated from the modifications made by the other transaction
States that any change in data by a completed transaction remains permanently in effect in the database
Slide 4 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
ADO.NET provides support for two types of transactions:Local transactions
Distributed transactions
Types of Transaction
A local transaction performs on a single data source. Because local transactions are performed on a single data source, these transactions are efficient to operate and easy to manage.
A distributed transaction performs on multiple data sources. Distributed transactions enable you to incorporate several distinct transactional operations into an atomic unit that either succeed or fail completely.
Slide 5 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
ADO.NET has an interface, IDbTransaction that contains methods for creating and performing local transactions against a single data source.
The following table lists the transaction classes available in the .NET Framework 2.0.
Performing Local Transactions
Types of Transaction Classes Description
System.Data.SqlClient.SqlTransaction Transaction class for .NET framework data provider for SQL Server
System.Data.OleDb.OleDbTransaction Transaction class for .NET framework data provider for OLE DB
System.Data.Odbc.OdbcTransaction Transaction class for .NET framework data provider for ODBC
System.Data.OracleClient.OracleTransaction Transaction class for .NET framework data provider for Oracle
Slide 6 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
Let us understand how to create a local transaction.string connectString =
"Initial Catalog=AdventureWorks; Data Source=SQLSERVER01;User id=sa;Password=niit#1234";
SqlConnection cn = new SqlConnection();
cn.Open();
cn = connectString;
SqlTransaction tran = null;
Performing Local Transactions (Contd.)
Creating a connection to the data source
Slide 7 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
try
{
tran = cn.BeginTransaction();SqlCommand cmd = new SqlCommand("INSERT INTOempdetails(ccode,cname, caddress,cstate,ccountry, cDesignation,cDepartment)VALUES(1101,'Linda Taylor', 'Oxfordshire','London','UK','Manager','Finance')", cn, tran);
Performing Local Transactions (Contd.)
Calling the BeginTransaction() methodCreating the command object and passing the SQL command
Slide 8 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
cmd.ExecuteNonQuery();
tran.Commit();
Console.WriteLine("Transaction Committed\n)";
}
catch (SqlException ex)
{
tran.Rollback();
Console.WriteLine("Error - TRANSACTION ROLLED BACK\n" + ex.Message);
}
Performing Local Transactions (Contd.)
Committing the transaction if the command succeeds
Rolling back the transaction if the command fails
Slide 9 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
catch (System.Exception ex)
{
Console.WriteLine("System Error\n" + ex.Message);
}
finally
{
cn.Close();
}
Console.ReadLine();
}
}
}
Performing Local Transactions (Contd.)
Slide 10 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
Which method is called when a transaction succeeds in its operation?1. Rollback()
2. Commit()
3. BeginTransaction()
4. ExecuteNonQuery()
Just a minute
Answer:2. Commit()
Slide 11 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
Problem Statement:John Howard, the HR executive has joined as a head of the Finance department at Texas. Therefore, his details need to be added to the HR database. The Department code for the Finance department is D002. The user name that needs to be assigned to him is JohnH and the password is howard.
As a part of the development team, you need to create a transaction that will allow you to add these details in the HRusers and Department tables.
Demo: Managing Local Transactions
Slide 12 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
Distributed transactions are performed on multiple data sources or multiple connections within a data source.
Distributed transactions are created in the System.Transaction namespace.
The System.Transaction namespace has a TransactionScope class, which enables a developer to create and manage distributed transactions.
To create a distributed transaction, a TransactionScope object is created in a using block.
The TransactionScope object decides whether to create a local transaction or a distributed transaction. This is known as transaction promotion.
Managing Distributed Transactions
Slide 13 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
Let us understand how to create a distributed transaction.using (TransactionScope ts = new TransactionScope())
{
using(SqlConnection cn = new SqlConnection("InitialCatalog=HR;Data Source=SQLSERVER01;Userid=sa;Password=niit#1234")){
cn.Open();
Managing Distributed Transactions (Contd.)
Creating a connection to the data source
Creating the TransactionScope object
Slide 14 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
using(SqlCommand cmd = newSqlCommand("INSERT INTO HRusers(cUserName,cPassword)VALUES('Darren','Cooper')", cn))
{
int rowsUpdated =cmd.ExecuteNonQuery();if (rowsUpdated > 0)
{
Managing Distributed Transactions (Contd.)
Creating a SqlCommand object to insert a record in the HRusers table
Slide 15 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
SqlConnection cn1 = newSqlConnection("Initial Catalog=HR;Data Source=SQLSERVER01;User id=sa;Password=niit#1234"))
{
cn1.Open();
using (SqlCommand cmd1 =new SqlCommand("DELETEDepartment WHEREcDepartmentCode=1111", cn1))
{
int rowsUpdated1 = cmd1.ExecuteNonQuery();
Managing Distributed Transactions (Contd.)
Creating another connection to the same data source
Creating another SqlCommand object to delete a record in the Department table
Slide 16 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
if (rowsUpdated1 > 0)
{
ts.Complete();
Console.WriteLine("Transaction Committed\n");
cn1.Close();
}
}}}
cn.Close();
}}
Console.ReadLine();
}}}
Managing Distributed Transactions (Contd.)
Calling the Complete() method to commit the transactions
Slide 17 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
The _______ method is invoked to commit the distributed transaction.1. Commit()
2. BeginTransaction()
3. Complete()
4. Rollback()
Just a minute
Answer:3. Complete()
Slide 18 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
Bulk copy operations can be performed as an isolated operation or as a part of a transaction.
By default, a bulk copy operation is its own transaction.
To perform a bulk copy operation, you need to create a new instance of BulkCopy class with a connection string.
The bulk copy operation creates, and then, commits or rolls back the transaction.
Performing Bulk Copy Operations in a Transaction
Slide 19 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
An isolation level determines the effect a transaction has on other transactions that are currently running, and vice versa.
By default, all transactions are completely isolated and run concurrently without impacting each other.
The isolation level of a transaction specifies the locking strategy used by the connection running the transaction to prevent concurrency problems when multiple transactions access the same data.
Specifying Isolation Levels of a Transaction
Slide 20 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
The following table describes the concurrency errors that can occur if multiple transactions access the same data at the same time.
Concurrency Error Description
Dirty read A transaction reads the data that has not been committed by the other transaction. This can create problem if a transaction that has added the data is rolled back.
Nonrepeatable read A transaction reads the same row more than once and a different transaction modifies the row between the reads.
Phantom read A transaction reads a rowset more than once and a different transaction inserts or deletes rows between the first transaction’s reads.
Specifying Isolation Levels of a Transaction (Contd.)
Slide 21 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
The various types of isolation levels of a transaction are:Read Uncommitted
Read Committed with Locks
Read Committed with Snapshots
Repeatable Read
Snapshot
Serializable
Specifying Isolation Levels of a Transaction (Contd.)
Slide 22 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
The following table describes the various isolation levels and the corresponding concurrency errors for a transaction.
Isolation Level Dirty Read Nonrepeatable Read Phantom Read
Read Uncommitted Yes Yes Yes
Read Committed with Locks
No Yes Yes
Read Committed with Snapshots
No Yes Yes
Repeatable Read No No Yes
Snapshot No No No
Serializable No No No
Specifying Isolation Levels of a Transaction (Contd.)
Slide 23 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
Let us understand how to set the isolation level of a transaction to Read Committed.TransactionOptions options = new TransactionOptions();
options.IsolationLevel =System.Transactions.IsolationLevel.ReadC ommitted;
using(TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, options)){
using (SqlConnection cn = newSqlConnection("Initial Catalog=HR;Data Source=SQLSERVER01;User id=sa;Password=niit#1234;")){
cn.Open();
Initializing the TransactionOptions object
Setting the Isolation level of both transactions to Read Committed
Creating a connection to a data source
Specifying Isolation Levels of a Transaction (Contd.)
Slide 24 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
using(SqlCommand cmd = new SqlCommand("INSERT INTO Department(cDepartmentCode ,vDepartmentName,vDepartmentHead,vLocation) VALUES(2013,'IT','Lara King','Houston')", cn)){
int rowsUpdated = cmd.ExecuteNonQuery();if (rowsUpdated > 0){
Creating a SqlCommand object to insert a record in the Department table
Specifying Isolation Levels of a Transaction (Contd.)
Slide 25 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
using (SqlConnection cn1 = new SqlConnection("Initial Catalog=HR;Data Source=SQLSERVER01;User id=sa;Password=niit#1234"))
{
cn1.Open();
using(SqlCommand cmd = new SqlCommand("INSERT INTO HRusers(cUserName,cPassword) VALUES('Hansel','Lord')", cn1))
Creating another connection to the same data source
Creating another command object to insert a record in the HRusers table
Specifying Isolation Levels of a Transaction (Contd.)
Slide 26 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
Calling the Complete() method to commit both transactions
Specifying Isolation Levels of a Transaction (Contd.)
{
int rowsUpdated1 = cmd1.ExecuteNonQuery();
if (rowsUpdated1 > 0)
{
ts.Complete();
}}}}
Slide 27 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
Which isolation level supports the occurrence of Dirty read, Nonrepeatable read, and Phantom read?1. Read Committed with Locks
2. Serializable
3. Read Committed with Snapshots
4. Read Uncommitted
Just a minute
Answer:4. Read Uncommitted
Slide 28 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
Problem Statement:Pamela Cruz has joined as an HR executive in Tebisco. Therefore, her details that includes user name and password needs to be inserted in the HRusers table. In addition, the details about her position needs to be added in the Position table. Both the operations need to be performed simultaneously in both the tables. As a part of the development team, you need to add the required details for Pamela in the HR database.
Note: To enable execution of the distributed transactions, you need to ensure that the MSDTC services are running on your system.
Demo: Managing Distributed Transactions
Slide 29 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
In this session, you learned that:A transaction is a logical unit of work that must be completed to maintain the consistency and integrity of a database.
A transaction has the following properties:Atomicity
Consistency
Isolation
Durability
The two types of transaction are:Local transactions
Distributed transactions
Summary
Slide 30 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
A local transaction performs on a single data source. The IDbTransaction interface contains methods for creating and performing local transactions against a data source.
A distributed transaction performs on multiple data sources. A distributed transaction enables you to incorporate several distinct transactional operations into an atomic unit that either succeed or fail completely.
Bulk copy operations can be performed as an isolated operations or as a part of a transaction. By default, a bulk copy operation is its own transaction.
An isolation level determines the effect a transaction has on other transactions that are currently running, and vice versa.
Summary (Contd.)
Slide 31 of 31Ver. 1.0
Developing Database Applications Using ADO.NET and XML
Session 10
The various concurrency errors that can occur when multiple transactions access the same data at the same time are:
Dirty read
Nonrepeatable read
Phantom read
The various types of isolation levels are for a transaction are:Read Uncommitted
Read Committed with Locks
Read Committed with Snapshots
Repeatable Read
Snapshot
Serializable
Summary (Contd.)