@@ -36,16 +36,19 @@ namespace Hangfire.PostgreSql
36
36
{
37
37
public class PostgreSqlMonitoringApi : IMonitoringApi
38
38
{
39
- private readonly NpgsqlConnection _connection ;
39
+ private readonly string _connectionString ;
40
+ private readonly Action < NpgsqlConnection > _connectionSetup ;
40
41
private readonly PostgreSqlStorageOptions _options ;
41
42
private readonly PersistentJobQueueProviderCollection _queueProviders ;
42
43
43
44
public PostgreSqlMonitoringApi (
44
- NpgsqlConnection connection ,
45
+ string connectionString ,
46
+ Action < NpgsqlConnection > connectionSetup ,
45
47
PostgreSqlStorageOptions options ,
46
48
PersistentJobQueueProviderCollection queueProviders )
47
49
{
48
- _connection = connection ?? throw new ArgumentNullException ( nameof ( connection ) ) ;
50
+ _connectionString = connectionString ?? throw new ArgumentNullException ( nameof ( connectionString ) ) ;
51
+ _connectionSetup = connectionSetup ;
49
52
_options = options ?? throw new ArgumentNullException ( nameof ( options ) ) ;
50
53
_queueProviders = queueProviders ?? throw new ArgumentNullException ( nameof ( queueProviders ) ) ;
51
54
}
@@ -387,7 +390,12 @@ SELECT COUNT(*)
387
390
} ) ;
388
391
}
389
392
390
- protected virtual NpgsqlConnection GetConnection ( ) => _connection ;
393
+ protected virtual NpgsqlConnection GetConnection ( )
394
+ {
395
+ var connection = new NpgsqlConnection ( _connectionString ) ;
396
+ _connectionSetup ? . Invoke ( connection ) ;
397
+ return connection ;
398
+ }
391
399
392
400
private Dictionary < DateTime , long > GetHourlyTimelineStats (
393
401
NpgsqlConnection connection ,
@@ -466,7 +474,11 @@ private IPersistentJobQueueMonitoringApi GetQueueApi(
466
474
467
475
private T UseConnection < T > ( Func < NpgsqlConnection , T > action )
468
476
{
469
- return action ( GetConnection ( ) ) ;
477
+ using ( var connection = GetConnection ( ) )
478
+ {
479
+ connection . Open ( ) ;
480
+ return action ( connection ) ;
481
+ }
470
482
}
471
483
472
484
private JobList < EnqueuedJobDto > EnqueuedJobs ( NpgsqlConnection connection , IEnumerable < long > jobIds )
0 commit comments