调用 DbConnection.EnlistTransaction 和设置 DbCommand.Transaction 有什么区别?

问题描述

我试图了解以下之间的区别:

我目前的理解:

  • DbConnection.EnlistTransaction:主要是建立一个连接到分布式事务,它继承自 DbTransaction
  • DbCommand.Transaction:是告诉在哪个事务(再次DbTransaction)中执行命令。

到目前为止,一切都很好,我只是引用了官方文档的内容

现在我想知道两者是如何结合的......意思是:

  • DbConnection.EnlistTransaction:需要在打开连接之前还是之后执行
  • DbCommand.Transaction,如果将DbConnection登记到分布式事务并创建DbCommand,是否意味着该命令直接从连接登记事务继承?但是,如果将另一个事务分配给命令,那么在命令执行时使用哪个事务呢?

编辑

关于DbConnection.EnlistTransactionhttps://stackoverflow.com/a/43998009/4636721

关于 DbConnection.BeginTransaction,它只是创建一个事务并调用 begin 方法,该方法只执行(非查询BEGIN TRANSACTION 语句。相反,Transaction.Commit 正在向后端发送 COMMIT 语句。

为了简单起见,我仅使用一些 System.Data.sqlite代码片段来说明我在说什么:

/// <summary>
/// Attempts to start a transaction.  An exception will be thrown if the transaction cannot
/// be started for any reason.
/// </summary>
/// <param name="deferredLock">TRUE to defer the writelock,or FALSE to lock immediately</param>
protected override void Begin(bool deferredLock)
{
  if (this._cnn._transactionLeveL++ != 0)
    return;
  try
  {
    using (sqliteCommand command = this._cnn.CreateCommand())
    {
      if (!deferredLock)
        command.CommandText = "BEGIN IMMEDIATE;";
      else
        command.CommandText = "BEGIN;";
      command.ExecuteNonQuery();
    }
  }
  catch (sqliteException ex)
  {
    --this._cnn._transactionLevel;
    this._cnn = (sqliteConnection) null;
    throw;
  }
}

/// <summary>Commits the current transaction.</summary>
public override void Commit()
{
  this.Checkdisposed();
  this.IsValid(true);
  if (this._cnn._transactionLevel - 1 == 0)
  {
    using (sqliteCommand command = this._cnn.CreateCommand())
    {
      command.CommandText = "COMMIT;";
      command.ExecuteNonQuery();
    }
  }
  --this._cnn._transactionLevel;
  this._cnn = (sqliteConnection) null;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...