From 9157a1ac9e1c27e56348cfc53be6f883322b1b15 Mon Sep 17 00:00:00 2001 From: Sergey Odinokov Date: Fri, 21 Jun 2024 15:25:26 +0700 Subject: [PATCH] Use QuerySingle* or ReadSingle* where possible to avoid allocating lists --- src/Hangfire.SqlServer/SqlServerConnection.cs | 4 ++-- src/Hangfire.SqlServer/SqlServerJobQueue.cs | 11 +++++------ .../SqlServerJobQueueMonitoringApi.cs | 2 +- src/Hangfire.SqlServer/SqlServerMonitoringApi.cs | 2 +- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Hangfire.SqlServer/SqlServerConnection.cs b/src/Hangfire.SqlServer/SqlServerConnection.cs index d968dc959..7e1a82803 100644 --- a/src/Hangfire.SqlServer/SqlServerConnection.cs +++ b/src/Hangfire.SqlServer/SqlServerConnection.cs @@ -227,7 +227,7 @@ public override JobData GetJobData(string id) { using (var multi = connection.QueryMultiple(sql, new { id = parsedId }, commandTimeout: _storage.CommandTimeout)) { - var jobData = multi.Read().SingleOrDefault(); + var jobData = multi.ReadSingleOrDefault(); if (jobData == null) return null; var parameters = multi.Read() @@ -285,7 +285,7 @@ public override StateData GetStateData(string jobId) return _storage.UseConnection(_dedicatedConnection, connection => { - var sqlState = connection.Query(sql, new { jobId = parsedId }, commandTimeout: _storage.CommandTimeout).SingleOrDefault(); + var sqlState = connection.QuerySingleOrDefault(sql, new { jobId = parsedId }, commandTimeout: _storage.CommandTimeout); if (sqlState == null) { return null; diff --git a/src/Hangfire.SqlServer/SqlServerJobQueue.cs b/src/Hangfire.SqlServer/SqlServerJobQueue.cs index 5a51a98f8..d257c175e 100644 --- a/src/Hangfire.SqlServer/SqlServerJobQueue.cs +++ b/src/Hangfire.SqlServer/SqlServerJobQueue.cs @@ -170,12 +170,11 @@ private SqlServerTimeoutJob FetchJob(string[] queues) parameters.Add("@timeoutSs", (int)_options.SlidingInvisibilityTimeout.Value.Negate().TotalSeconds); var fetchedJob = connection - .Query( + .QuerySingleOrDefault( GetNonBlockingFetchSql(), parameters, - commandTimeout: _storage.CommandTimeout) - .SingleOrDefault(); - + commandTimeout: _storage.CommandTimeout); + return fetchedJob != null ? new SqlServerTimeoutJob(_storage, fetchedJob.Id, fetchedJob.JobId.ToString(CultureInfo.InvariantCulture), fetchedJob.Queue, fetchedJob.FetchedAt.Value) : null; @@ -220,13 +219,13 @@ private SqlServerTransactionJob DequeueUsingTransaction(string[] queues, Cancell { transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted); - fetchedJob = connection.Query( + fetchedJob = connection.QuerySingleOrDefault( fetchJobSqlTemplate, #pragma warning disable 618 new { queues = queues, timeout = _options.InvisibilityTimeout.Negate().TotalSeconds }, #pragma warning restore 618 transaction, - commandTimeout: _storage.CommandTimeout).SingleOrDefault(); + commandTimeout: _storage.CommandTimeout); if (fetchedJob != null) { diff --git a/src/Hangfire.SqlServer/SqlServerJobQueueMonitoringApi.cs b/src/Hangfire.SqlServer/SqlServerJobQueueMonitoringApi.cs index 7e2ce61bc..befc36572 100644 --- a/src/Hangfire.SqlServer/SqlServerJobQueueMonitoringApi.cs +++ b/src/Hangfire.SqlServer/SqlServerJobQueueMonitoringApi.cs @@ -124,7 +124,7 @@ select sum(Enqueued) as EnqueuedCount, sum(Fetched) as FetchedCount return _storage.UseConnection(null, connection => { - var result = connection.Query(sqlQuery, new { queue = queue }).Single(); + var result = connection.QuerySingle(sqlQuery, new { queue = queue }); return new EnqueuedAndFetchedCountDto { diff --git a/src/Hangfire.SqlServer/SqlServerMonitoringApi.cs b/src/Hangfire.SqlServer/SqlServerMonitoringApi.cs index 4eef88337..ba6448533 100644 --- a/src/Hangfire.SqlServer/SqlServerMonitoringApi.cs +++ b/src/Hangfire.SqlServer/SqlServerMonitoringApi.cs @@ -361,7 +361,7 @@ public override JobDetailsDto JobDetails(string jobId) using (var multi = connection.QueryMultiple(sql, new { id = jobId }, commandTimeout: _storage.CommandTimeout)) { - var job = multi.Read().SingleOrDefault(); + var job = multi.ReadSingleOrDefault(); if (job == null) return null; var parameters = multi.Read()