27
27
using System . Transactions ;
28
28
using Hangfire . Annotations ;
29
29
using Hangfire . Logging ;
30
+ using Hangfire . PostgreSql . Utils ;
30
31
using Hangfire . Server ;
31
32
using Hangfire . Storage ;
32
33
using Npgsql ;
@@ -119,13 +120,9 @@ public PostgreSqlStorage(NpgsqlConnection existingConnection, PostgreSqlStorageO
119
120
}
120
121
121
122
NpgsqlConnectionStringBuilder connectionStringBuilder = new ( existingConnection . ConnectionString ) ;
122
-
123
- if ( ! options . EnableTransactionScopeEnlistment )
123
+ if ( ! options . EnableTransactionScopeEnlistment && connectionStringBuilder . Enlist )
124
124
{
125
- if ( connectionStringBuilder . Enlist )
126
- {
127
- throw new ArgumentException ( $ "TransactionScope enlistment must be enabled by setting { nameof ( PostgreSqlStorageOptions ) } .{ nameof ( options . EnableTransactionScopeEnlistment ) } to `true`.") ;
128
- }
125
+ throw new ArgumentException ( $ "TransactionScope enlistment must be enabled by setting { nameof ( PostgreSqlStorageOptions ) } .{ nameof ( options . EnableTransactionScopeEnlistment ) } to `true`.") ;
129
126
}
130
127
131
128
_existingConnection = existingConnection ;
@@ -134,23 +131,8 @@ public PostgreSqlStorage(NpgsqlConnection existingConnection, PostgreSqlStorageO
134
131
InitializeQueueProviders ( ) ;
135
132
}
136
133
137
- public PostgreSqlStorage ( NpgsqlConnection existingConnection )
134
+ public PostgreSqlStorage ( NpgsqlConnection existingConnection ) : this ( existingConnection , new PostgreSqlStorageOptions ( ) )
138
135
{
139
- if ( existingConnection == null )
140
- {
141
- throw new ArgumentNullException ( nameof ( existingConnection ) ) ;
142
- }
143
-
144
- NpgsqlConnectionStringBuilder connectionStringBuilder = new ( existingConnection . ConnectionString ) ;
145
- if ( connectionStringBuilder . Enlist )
146
- {
147
- throw new ArgumentException ( $ "TransactionScope enlistment must be enabled by setting { nameof ( PostgreSqlStorageOptions ) } .{ nameof ( PostgreSqlStorageOptions . EnableTransactionScopeEnlistment ) } to `true`.") ;
148
- }
149
-
150
- _existingConnection = existingConnection ;
151
- Options = new PostgreSqlStorageOptions ( ) ;
152
-
153
- InitializeQueueProviders ( ) ;
154
136
}
155
137
156
138
public PersistentJobQueueProviderCollection QueueProviders { get ; internal set ; }
@@ -205,16 +187,6 @@ public override string ToString()
205
187
}
206
188
}
207
189
208
- /// <summary>
209
- /// Timezone must be UTC for compatibility with Npgsql 6 and our usage of "timestamp without time zone" columns
210
- /// See https://github.com/frankhommers/Hangfire.PostgreSql/issues/221
211
- /// </summary>
212
- /// <param name="connectionStringBuilder">The ConnectionStringBuilder to set the Timezone property for</param>
213
- internal static void SetTimezoneToUtcForNpgsqlCompatibility ( NpgsqlConnectionStringBuilder connectionStringBuilder )
214
- {
215
- connectionStringBuilder . Timezone = "UTC" ;
216
- }
217
-
218
190
internal NpgsqlConnection CreateAndOpenConnection ( )
219
191
{
220
192
NpgsqlConnection connection ;
@@ -225,12 +197,13 @@ internal NpgsqlConnection CreateAndOpenConnection()
225
197
226
198
if ( ! Options . EnableTransactionScopeEnlistment )
227
199
{
228
- NpgsqlConnectionStringBuilder connectionStringBuilder ;
200
+ NpgsqlConnectionStringBuilder connectionStringBuilder =
229
201
#if ! USING_NPGSQL_VERSION_5
230
- connectionStringBuilder = connection . Settings ;
202
+ connection . Settings ;
231
203
#else
232
- connectionStringBuilder = new ( connection . ConnectionString ) ;
204
+ new ( connection . ConnectionString ) ;
233
205
#endif
206
+
234
207
if ( connectionStringBuilder . Enlist )
235
208
{
236
209
throw new ArgumentException (
@@ -284,7 +257,7 @@ internal T UseTransaction<T>(DbConnection dedicatedConnection,
284
257
285
258
if ( ! EnvironmentHelpers . IsMono ( ) )
286
259
{
287
- using TransactionScope transaction = CreateTransaction ( isolationLevel ) ;
260
+ using TransactionScope transaction = CreateTransactionScope ( isolationLevel ) ;
288
261
T result = UseConnection ( dedicatedConnection , connection => {
289
262
connection . EnlistTransaction ( Transaction . Current ) ;
290
263
return func ( connection , null ) ;
@@ -358,12 +331,9 @@ private static void Current_TransactionCompleted(object sender, TransactionEvent
358
331
}
359
332
}
360
333
361
- private static TransactionScope CreateTransaction ( IsolationLevel ? isolationLevel )
334
+ internal TransactionScope CreateTransactionScope ( IsolationLevel ? isolationLevel , TimeSpan ? timeout = null )
362
335
{
363
- return isolationLevel != null
364
- ? new TransactionScope ( TransactionScopeOption . Required ,
365
- new TransactionOptions { IsolationLevel = isolationLevel . Value } )
366
- : new TransactionScope ( ) ;
336
+ return TransactionHelpers . CreateTransactionScope ( isolationLevel , Options . EnableTransactionScopeEnlistment , timeout ) ;
367
337
}
368
338
369
339
private static System . Data . IsolationLevel ? ConvertIsolationLevel ( IsolationLevel ? isolationLevel )
@@ -448,7 +418,7 @@ private NpgsqlConnectionStringBuilder SetupConnectionStringBuilderParameters(Npg
448
418
if ( ! Options . EnableTransactionScopeEnlistment )
449
419
{
450
420
builder . Enlist = false ;
451
- SetTimezoneToUtcForNpgsqlCompatibility ( builder ) ;
421
+ builder . SetTimezoneToUtcForNpgsqlCompatibility ( ) ;
452
422
}
453
423
454
424
return builder ;
0 commit comments