JobStorage.Current.Monitoring API is creating multiple Database Connections

I have a background service to monitor the Hangfire Server e.g. Check if some jobs are stuck in processing or check if the number enqueued jobs are greater than a set threshold.
Background Service runs every 3 mins and initialized the monitoringAPI instance and checks the required conditions. If those are true it just sends an alert email.
Issue is after sometime e.g. 5-10 mins, the number of database connections increases to 50-60 at max. After that I get ‘Too Many Connections’ exception while connecting to DB. I can increase the max number of connections to my DB but what is the issue with MonitoringAPI? What it is creating connections and not closing it afterwards?
I have tried to dispose the monitoring API variable but it’s not disposable. I have tried to add some checks to not initialize monitoring API if it already exists but still it keeps on creating the DB connections.

Any solutions or tips?

Thanks in advance :slight_smile:

Same here, Also on BackgroundJob.Enqueue, etc

SqlServerMonitoringApi uses connection with a method-based scope, and takes and releases it to a connection pool in the same method, that’s why it’s not required for monitoring API class/interface to be disposable.

SqlServerConnection class can obtain a connection for a longer time when there are active distributed locks, but everything is using a single API for handling connections. There was a possible leak in Hangfire 1.7.9, but I don’t know any outstanding issues with this – every connection should be disposed.