@@ -209,7 +209,8 @@ public void init(
209
209
protected <T , E extends Exception > T executeWithSubscribedPlugins (
210
210
final String methodName ,
211
211
final PluginPipeline <T , E > pluginPipeline ,
212
- final JdbcCallable <T , E > jdbcMethodFunc )
212
+ final JdbcCallable <T , E > jdbcMethodFunc ,
213
+ final @ Nullable ConnectionPlugin pluginToSkip )
213
214
throws E {
214
215
215
216
if (pluginPipeline == null ) {
@@ -232,7 +233,7 @@ protected <T, E extends Exception> T executeWithSubscribedPlugins(
232
233
throw new RuntimeException ("Error processing this JDBC call." );
233
234
}
234
235
235
- return pluginChainFunc .call (pluginPipeline , jdbcMethodFunc );
236
+ return pluginChainFunc .call (pluginPipeline , jdbcMethodFunc , pluginToSkip );
236
237
}
237
238
238
239
@@ -258,20 +259,28 @@ protected <T, E extends Exception> PluginChainJdbcCallable<T, E> makePluginChain
258
259
final ConnectionPlugin plugin = this .plugins .get (i );
259
260
final Set <String > pluginSubscribedMethods = plugin .getSubscribedMethods ();
260
261
final String pluginName = pluginNameByClass .getOrDefault (plugin .getClass (), plugin .getClass ().getSimpleName ());
261
- final boolean isSubscribed =
262
- pluginSubscribedMethods .contains (ALL_METHODS )
263
- || pluginSubscribedMethods .contains (methodName );
262
+ final boolean isSubscribed = pluginSubscribedMethods .contains (ALL_METHODS )
263
+ || pluginSubscribedMethods .contains (methodName );
264
264
265
265
if (isSubscribed ) {
266
266
if (pluginChainFunc == null ) {
267
- pluginChainFunc = (pipelineFunc , jdbcFunc ) ->
267
+ // This case is for DefaultConnectionPlugin that always terminates the list of plugins.
268
+ // Default plugin can't be skipped.
269
+ pluginChainFunc = (pipelineFunc , jdbcFunc , pluginToSkip ) ->
268
270
executeWithTelemetry (() -> pipelineFunc .call (plugin , jdbcFunc ), pluginName );
269
271
} else {
270
272
final PluginChainJdbcCallable <T , E > finalPluginChainFunc = pluginChainFunc ;
271
- pluginChainFunc = (pipelineFunc , jdbcFunc ) ->
272
- executeWithTelemetry (() -> pipelineFunc .call (
273
- plugin , () -> finalPluginChainFunc .call (pipelineFunc , jdbcFunc )),
273
+ pluginChainFunc = (pipelineFunc , jdbcFunc , pluginToSkip ) -> {
274
+ if (pluginToSkip == plugin ) {
275
+ return finalPluginChainFunc .call (pipelineFunc , jdbcFunc , pluginToSkip );
276
+ } else {
277
+ return executeWithTelemetry (
278
+ () -> pipelineFunc .call (
279
+ plugin ,
280
+ () -> finalPluginChainFunc .call (pipelineFunc , jdbcFunc , pluginToSkip )),
274
281
pluginName );
282
+ }
283
+ };
275
284
}
276
285
}
277
286
}
@@ -338,7 +347,8 @@ public <T, E extends Exception> T execute(
338
347
(plugin , func ) ->
339
348
plugin .execute (
340
349
resultType , exceptionClass , methodInvokeOn , methodName , func , jdbcMethodArgs ),
341
- jdbcMethodFunc );
350
+ jdbcMethodFunc ,
351
+ null );
342
352
}
343
353
344
354
/**
@@ -359,6 +369,7 @@ public <T, E extends Exception> T execute(
359
369
* @param isInitialConnection a boolean indicating whether the current {@link Connection} is
360
370
* establishing an initial physical connection to the database or has
361
371
* already established a physical connection in the past
372
+ * @param pluginToSkip the plugin that needs to be skipped while executing this pipeline
362
373
* @return a {@link Connection} to the requested host
363
374
* @throws SQLException if there was an error establishing a {@link Connection} to the requested
364
375
* host
@@ -367,7 +378,8 @@ public Connection connect(
367
378
final String driverProtocol ,
368
379
final HostSpec hostSpec ,
369
380
final Properties props ,
370
- final boolean isInitialConnection )
381
+ final boolean isInitialConnection ,
382
+ final @ Nullable ConnectionPlugin pluginToSkip )
371
383
throws SQLException {
372
384
373
385
TelemetryContext context = telemetryFactory .openTelemetryContext ("connect" , TelemetryTraceLevel .NESTED );
@@ -378,7 +390,8 @@ public Connection connect(
378
390
plugin .connect (driverProtocol , hostSpec , props , isInitialConnection , func ),
379
391
() -> {
380
392
throw new SQLException ("Shouldn't be called." );
381
- });
393
+ },
394
+ pluginToSkip );
382
395
} catch (final SQLException | RuntimeException e ) {
383
396
throw e ;
384
397
} catch (final Exception e ) {
@@ -403,6 +416,7 @@ public Connection connect(
403
416
* @param isInitialConnection a boolean indicating whether the current {@link Connection} is
404
417
* establishing an initial physical connection to the database or has
405
418
* already established a physical connection in the past
419
+ * @param pluginToSkip the plugin that needs to be skipped while executing this pipeline
406
420
* @return a {@link Connection} to the requested host
407
421
* @throws SQLException if there was an error establishing a {@link Connection} to the requested
408
422
* host
@@ -411,7 +425,8 @@ public Connection forceConnect(
411
425
final String driverProtocol ,
412
426
final HostSpec hostSpec ,
413
427
final Properties props ,
414
- final boolean isInitialConnection )
428
+ final boolean isInitialConnection ,
429
+ final @ Nullable ConnectionPlugin pluginToSkip )
415
430
throws SQLException {
416
431
417
432
try {
@@ -421,7 +436,8 @@ public Connection forceConnect(
421
436
plugin .forceConnect (driverProtocol , hostSpec , props , isInitialConnection , func ),
422
437
() -> {
423
438
throw new SQLException ("Shouldn't be called." );
424
- });
439
+ },
440
+ pluginToSkip );
425
441
} catch (SQLException | RuntimeException e ) {
426
442
throw e ;
427
443
} catch (Exception e ) {
@@ -535,7 +551,8 @@ public void initHostProvider(
535
551
},
536
552
() -> {
537
553
throw new SQLException ("Shouldn't be called." );
538
- });
554
+ },
555
+ null );
539
556
} finally {
540
557
context .closeContext ();
541
558
}
@@ -632,13 +649,16 @@ public ConnectionProvider getEffectiveConnProvider() {
632
649
return this .effectiveConnProvider ;
633
650
}
634
651
635
- private interface PluginPipeline <T , E extends Exception > {
652
+ protected interface PluginPipeline <T , E extends Exception > {
636
653
637
654
T call (final @ NonNull ConnectionPlugin plugin , final @ Nullable JdbcCallable <T , E > jdbcMethodFunc ) throws E ;
638
655
}
639
656
640
- private interface PluginChainJdbcCallable <T , E extends Exception > {
657
+ protected interface PluginChainJdbcCallable <T , E extends Exception > {
641
658
642
- T call (final @ NonNull PluginPipeline <T , E > pipelineFunc , final @ NonNull JdbcCallable <T , E > jdbcMethodFunc ) throws E ;
659
+ T call (
660
+ final @ NonNull PluginPipeline <T , E > pipelineFunc ,
661
+ final @ NonNull JdbcCallable <T , E > jdbcMethodFunc ,
662
+ final @ Nullable ConnectionPlugin pluginToSkip ) throws E ;
643
663
}
644
664
}
0 commit comments