问题描述
我试图了解以下之间的区别:
-
DbConnection.EnlistTransaction
(和related bits of info) DbCommand.Transaction
-
DbTransaction
这是您通过调用DbConnection.BeginTransaction
或 Transaction.Current 获得的信息
我目前的理解:
-
DbConnection.EnlistTransaction
:主要是建立一个连接到分布式事务,它继承自DbTransaction
。 -
DbCommand.Transaction
:是告诉在哪个事务(再次DbTransaction
)中执行命令。
到目前为止,一切都很好,我只是引用了官方文档的内容。
现在我想知道两者是如何结合的......意思是:
-
DbConnection.EnlistTransaction
:需要在打开连接之前还是之后执行? -
DbCommand.Transaction
,如果将DbConnection
登记到分布式事务并创建DbCommand
,是否意味着该命令直接从连接登记事务继承?但是,如果将另一个事务分配给命令,那么在命令执行时使用哪个事务呢?
编辑
关于DbConnection.EnlistTransaction
:https://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 (将#修改为@)