gpt4 book ai didi

c# - 如何为 SqlTransaction.Save() 设置超时?

转载 作者:行者123 更新时间:2023-11-30 15:22:16 24 4
gpt4 key购买 nike

我得到:

System.Data.SqlClient.SqlException (0x80131904): Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalTransaction.Save(String savePointName)
at System.Data.SqlClient.SqlTransaction.Save(String savePointName)

如何增加 SqlTransaction.Save() 的超时时间?

最佳答案

现在我真的希望我没有鼓励 PSVSupporter 删除他的答案(如果他取消删除,请给他投票)。

在内部,当您发出任何事务请求时,该请求使用基础连接的 ConnectionTimeout 属性作为查询的 CommandTimeout

你可以在源码中遵循这个过程,重要的部分在SqlInternalConnectionTds.ExecuteTransactionYukonSqlInternalConnectionTds.ExecuteTransactionPreYukon 都通过ConnectionOptions.ConnectTimeout在它们调用的函数的 timeout 参数中。因此,要解决您的问题,请在创建连接时使用更大的 ConnectionTimeout

SqlTransaction

    public void Save(string savePointName) {
//SNIP
_internalTransaction.Save(savePointName);
//SNIP
}

SqlInternalTransaction

   internal void Save(string savePointName) {
//SNIP
_innerConnection.ExecuteTransaction(SqlInternalConnection.TransactionRequest.Save, savePointName, IsolationLevel.Unspecified, null, false);
//SNIP
}

SqlInternalConnectionTds

    override internal void ExecuteTransaction(TransactionRequest transactionRequest, string name, IsolationLevel iso, SqlInternalTransaction internalTransaction, bool isDelegateControlRequest) {
//SNIP

if (!_parser.IsYukonOrNewer) {
ExecuteTransactionPreYukon(transactionRequest, transactionName, iso, internalTransaction);
}
else {
ExecuteTransactionYukon(transactionRequest, transactionName, iso, internalTransaction, isDelegateControlRequest);
}
}

internal void ExecuteTransactionPreYukon(
TransactionRequest transactionRequest,
string transactionName,
IsolationLevel iso,
SqlInternalTransaction internalTransaction) {
//SNIP

Threading.Tasks.Task executeTask = _parser.TdsExecuteSQLBatch(sqlBatch.ToString(),
ConnectionOptions.ConnectTimeout, null, _parser._physicalStateObj, sync: true);

//SNIP
}

internal void ExecuteTransactionYukon(
TransactionRequest transactionRequest,
string transactionName,
IsolationLevel iso,
SqlInternalTransaction internalTransaction,
bool isDelegateControlRequest) {
//SNIP

_parser.TdsExecuteTransactionManagerRequest(null, requestType, transactionName, isoLevel,
ConnectionOptions.ConnectTimeout, internalTransaction, stateObj, isDelegateControlRequest);

//SNIP
}

关于c# - 如何为 SqlTransaction.Save() 设置超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35993335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com