【发布时间】:2026-01-21 01:20:04
【问题描述】:
使用BeginTransaction("MyTransactionName")创建SqlTransaction对象时,有没有办法从返回的事务对象中获取事务的名称?
想法是当我的事务失败时,我想记录事务的名称,因为系统是多线程的,并且会发生一些需要处理的死锁。
我的示例代码中的logger 对象是我们自己的组件,这是一个非常简单的示例,因为很多代码无法在此处发布。
using (SqlConnection myConnection = new SqlConnection(SQLConnectString)) {
myConnection.Open();
SqlTransaction transaction = myConnection.BeginTransaction("UpdateTransaction");
//The update methods are omitted here for brevity
try {
logger.Log("Attempt commit ");
transaction.Commit();
} catch (Exception ex) {
logger.Error("Commit failed - " + ex.Message, ex.StackTrace);
//At this point, I want to write the name of the failed
//transaction into the log.
}
}
任何帮助将不胜感激。
【问题讨论】:
标签: c# sql-server transactions