آشنایی با TransactionScope
نویسنده: مسعود پاکدل
تاریخ: ۱۳۹۲/۰۳/۰۹ ۰:۱۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using ( TransactionScope scope = new TransactionScope() )
{
//Statement1
//Statement2
//Statement3
scope.Complete();
}using ( System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope() )
{
if ( result == 0 )
{
throw new ApplicationException();
}
scope.Complete();
}var defaultTimeout = TransactionManager.DefaultTimeout var maxTimeout = TransactionManager.MaximumTimeout
TransactionOptions option = new TransactionOptions(); option.Timeout = TimeSpan.MaxValue;
using ( System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope(TransactionScopeOption.Required ,option) ) { scope.Complete(); }
using(TransactionScope scope1 = new TransactionScope())
{
try
{
using(TransactionScope scope2 = new TransactionScope(TransactionScopeOption.Suppress))
{
//به دلیل استفاده از Suppress این محدوده خارج از تراکنش محسوب میشود
}
//شروع محدوده تراکنش
}
catch
{}
//Rest of scope1
}using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
string strCmd = "SQL to Execute";
conn = new SqlClient.SqlConnection("Connection to DB1");
conn.Open()
objCmd = new SqlClient.SqlCommand(strCmd, conn);
objCmd.ExecuteNonQuery();
string strCmd2 = "SQL to Execute";
conn2 = new SqlClient.SqlConnection("Connection to DB2");
conn2.Open()
objCmd2 = new SqlClient.SqlCommand(strCmd2, conn2);
objCmd2.ExecuteNonQuery();
}using(TransactionScope scope1 = new TransactionScope())
{
using(TransactionScope scope2 = new TransactionScope(TransactionScopeOption.Required))
{
...
}
using(TransactionScope scope3 = new TransactionScope(TransactionScopeOption.RequiresNew))
{
...
}
using(TransactionScope scope4 = new TransactionScope(TransactionScopeOption.Suppress))
{
...
}
} public void Save( TEntity entity )
{
DbTransaction transaction = null;
try
{
transaction = this.Database.Connection.BeginTransaction();
//عملیات مورد نظر
transaction.Commit();
}
catch
{
transaction.Rollback();
}
finally
{
transaction.Dispose();
}
}SaveChanges operates within a transaction. SaveChanges will roll back that transaction and throw an exception if any of the dirty ObjectStateEntry objects cannot be persisted.
context.Connection.Open();
DEPT department = context.DEPT.Where(d => d.DEPTNO == 10).First();
department.LOC = "TEST";
using (System.Data.Common.DbTransaction transaction = context.Connection.BeginTransaction())
{
context.SaveChanges();
department.DNAME = "TEST";
context.SaveChanges();
if(flag)
{
transaction.Commit();
}
else
{
transaction.Rollback();
}
} DEPT department = context.DEPT.Where(d => d.DEPTNO == 25).First();
department.DNAME = "xxx";
using (TransactionScope tscope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
context.SaveChanges();
department.DNAME = "TEST";
context.SaveChanges();
if(flag)
tscope.Complete();
}context.Connection.Open(); DEPT department = context.DEPT.Where(d => d.DEPTNO == 10).First(); department.LOC = "TEST"; using (System.Data.Common.DbTransaction transaction = context.SaveChanges(); department.DNAME = "TEST"; context.SaveChanges(); }
context.Database.UseTransaction(null);
context.Database.ExecuteSqlCommand("ALTER DATABASE FDb_20120 COLLATE Persian_100_CS_AI");
Error Message : ALTER DATABASE statement not allowed within multi-statement transaction. The ALTER DATABASE statement must run in autocommit mode (the default transaction management mode) and is not allowed in an explicit or implicit transaction.
public interface IUnitOfWork
{
void Dispose();
DbSet<T> Set<T>() where T : class;
int SaveChanges();
Database Database { get; }
}