2

一般来说,是否有一个接口表明TransactionScope-awareness?是否有可用的常用数据库列表TransactionScope?(它是扩展DbConnection或实现IDbConnection的任何东西吗?)

TransactionScope与合作的数据库有什么关系?即,是否TransactionScope以某种方式搜索活动连接并调用适当的开始/提交/回滚方法?数据库是否寻找环境TransactionScope变量?或两者?

如有必要,背景。我正在处理一个旧的 DAL,它利用了一些不同的类似事务的策略:

//
// transactionless queries
//

// Microsoft.Practices.EnterpriseLibrary.Data. ... 
Database db = SQLHelper.GetDatabase();
DbCommand cmd = db.GetStoredProcComment("doSomething");
db.AddInParameter(cmd, "p1", DbType.Guid, Id);
using (IDataReader r = db.ExecuteReader())
{
  // etc.
}


//
// Transaction objects being passed around and attached to DbCommands
//

// somewhere up the line ... 
DbConnection con = TransactionScopeConnections.GetConnection(db);
DbTransaction transaction = con.BeginTransaction();

// Microsoft.Practices.EnterpriseLibrary.Data. ... 
Database db = SQLHelper.GetDatabase();
DbConnection con = TransactionScopeConnections.GetConnections(db);
DbCommand cmd = db.GetStoredProcComment("doSomething");
cmd.Transaction = transaction;
db.AddInParameter(cmd, "p1", DbType.Guid, Id);
using (IDataReader r = db.ExecuteReader())
{
  // etc.
}


//
// queries in TransactionScopes
//

using (TransactionScope scope = new TransactionScope())
{
  // Microsoft.Practices.EnterpriseLibrary.Data. ... 
  Database db = SQLHelper.GetDatabase();
  DbCommand cmd = db.GetStoredProcComment("doSomething");
  db.AddInParameter(cmd, "p1", DbType.Guid, Id);
  using (IDataReader r = db.ExecuteReader())
  {
    // etc.
  }
  scope.Complete();
}


//
// both
//

Database db = SQLHelper.GetDatabase();
using (TransactionScope transactionScope = new TransactionScope())
{
    DbConnection con = TransactionScopeConnections.GetConnection(db);
    DbTransaction transaction = con.BeginTransaction();

    /* ... build query ... */

    query.transaction = transaction;

    /* ... execute query, read results, if any ... */

    transaction.Commit();
    transactionScope.Complete();

}

这些都是企业库的有效选项吗?TransactionScope表格没有意义吗?最后一种形式可能是多余的吗?

4

1 回答 1

0

要回答您的问题,是否有指示与 TransactionScope 兼容的接口?还是利用 TransactionScope 的数据库类型对象的列表?

是的,您可以考虑使用IDBTransaction接口来覆盖 .NET 框架数据提供者访问关系数据库的实现。

TransactionScope当您希望将事务提升为完全分布式事务(例如 MSDTC)时,我看到的使用方式。TransactionScope本身并不仅限于ADO.NET,可以参考Scope and More

于 2013-04-19T02:36:52.797 回答