客户定期出现“不支持嵌套/并发事务”。错误。在我这边不可重复,但试图了解它是如何发生的。它只发生在以下方法中:
public DataTable ExecuteDataTable(CommandType type, string query, params NpgsqlParameter[] parameters)
{
NpgsqlCommand command = PrepareCommand(type, query, parameters);
// Use transaction for functions with refcursors. This is necessary to prevent cursors
// returned by refcursor function from closing after the implicity transaction is finished
// (just after we do the function call).
NpgsqlTransaction t = sqlConnection.BeginTransaction();
try
{
NpgsqlDataAdapter da = new NpgsqlDataAdapter(command);
System.Data.DataSet ds = new System.Data.DataSet();
// Fill the DataSet using default values for DataTable names, etc
da.Fill(ds);
t.Commit();
// Detach the SqlParameters from the command object, so they can be used again
command.Parameters.Clear();
return ds.Tables[0];
}
catch (Exception e)
{
// Commit all transactions so connection can be used again (Npgsql does not allow nested transactions)
t.Rollback();
// Log error and problem query
LogError(e);
// Throw same exception to a higher level for correct handling
throw e;
}
}
如果出现错误,我总是尝试回滚以避免嵌套事务,但是它会在某些安装中定期发生。是 Npgsql 问题还是实现中的问题?这怎么可能发生?如果 2 个进程同时从不同的类、应用程序运行此方法,这是否与连接池有关?
调用堆栈:
2013-10-08 13:00:27.6038|Common.Logging.NLog.NLogLogger.WriteInternal|Info|Job DEFAULT.SQLServerSyncScheduledJob 抛出 JobExecutionException:|System.InvalidOperationException:不支持嵌套/并发事务。在 Npgsql.NpgsqlConnection.BeginTransaction(IsolationLevel 级别) 在 Npgsql.NpgsqlConnection.BeginTransaction() 在 DbFramework.PgDataAccess.ExecuteDataTable(CommandType 类型,字符串查询,NpgsqlParameter[] 参数)