Download Josyala - ODU Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Global serializability wikipedia , lookup

Database model wikipedia , lookup

Clusterpoint wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Object-relational impedance mismatch wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Commitment ordering wikipedia , lookup

Concurrency control wikipedia , lookup

Serializability wikipedia , lookup

Transcript
DISTRIBUTED
TRANSACTIONS IN .NET
Presented By Divya Josyala.
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
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.
Overview

Local Transaction: transactions are
performed on a single database
Transfer(
)
Charge()
Credit()
database
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()
Charge()
Database1
Credit()
Database2
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
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.
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.
TransactionOptions
// Create the TransactionOptions object
TransactionOptions TranOpt = new TransactionOptions();
// Set the Isolation Level
TranOpt.IsolationLevel =
System.Transactions.IsolationLevel.ReadCommitted;
// Set the timeout to be 2 minutes
// Uses the (hours, minutes, seconds) constructor
TimeSpan Time = new TimeSpan(0, 2, 0);
TranOpt.Timeout = Time;
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.
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
}
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]
WebService - DEPT
[WebMethod(false, TransactionOption.Required)]
public int AddDept(string deptName, string location)
{
try
{
string connString =
System.Configuration.ConfigurationManager.ConnectionStrings
["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;
WebService - DEPT
//Add the DeptName parameter
SqlParameter paramDeptName = new
SqlParameter("@DeptName", SqlDbType.VarChar, 50);
paramDeptName.Value = deptName;
paramDeptName.Direction = ParameterDirection.Input;
command.Parameters.Add(paramDeptName);
//Add the Location parameter
SqlParameter paramLocation = new
SqlParameter("@Location", SqlDbType.VarChar, 50);
paramLocation.Value = location;
paramLocation.Direction = ParameterDirection.Input;
command.Parameters.Add(paramLocation);
//Add the DeptNo parameter as the Output parameter
SqlParameter paramDeptNo = new
SqlParameter("@DeptNo", SqlDbType.Int, 4);
paramDeptNo.Direction = ParameterDirection.Output;
command.Parameters.Add(paramDeptNo);
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["@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;
}
}
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,empAddres,deptNo);
int empNo = emp.AddEmp(empName, empAddres, 200);
return true;
}
catch (Exception e)
{
throw e;
}
REFERENCES
MSDN - DATA POINTS – By John Papa
Url: http://msdn.microsoft.com/msdnmag/issues/05/02/DataPoints/
Develop Transactional .Net Web Services – By Thiru Thangarathinam
Url: http://www.developer.com/net/asp/article.php/3385631
Comments / Questions ??

THE END