1616import java .sql .PreparedStatement ;
1717import java .sql .Statement ;
1818import java .util .Map ;
19+ import java .util .function .Supplier ;
1920import javax .annotation .Nullable ;
2021
2122public class JdbcAdviceScope {
@@ -32,24 +33,22 @@ private JdbcAdviceScope(CallDepth callDepth, DbRequest request, Context context,
3233 }
3334
3435 public static JdbcAdviceScope startBatch (CallDepth callDepth , Statement statement ) {
35- return start (callDepth , null , statement , null );
36+ return start (callDepth , () -> createBatchRequest ( statement ) );
3637 }
3738
3839 public static JdbcAdviceScope startStatement (
3940 CallDepth callDepth , String sql , Statement statement ) {
40- return start (callDepth , sql , statement , null );
41+ return start (callDepth , () -> DbRequest . create ( statement , sql ) );
4142 }
4243
4344 public static JdbcAdviceScope startPreparedStatement (
4445 CallDepth callDepth , PreparedStatement preparedStatement ) {
45- return start (callDepth , null , null , preparedStatement );
46+ return start (
47+ callDepth ,
48+ () -> DbRequest .create (preparedStatement , JdbcData .getParameters (preparedStatement )));
4649 }
4750
48- private static JdbcAdviceScope start (
49- CallDepth callDepth ,
50- @ Nullable String sql ,
51- @ Nullable Statement statement ,
52- @ Nullable PreparedStatement preparedStatement ) {
51+ private static JdbcAdviceScope start (CallDepth callDepth , Supplier <DbRequest > requestSupplier ) {
5352 // Connection#getMetaData() may execute a Statement or PreparedStatement to retrieve DB info
5453 // this happens before the DB CLIENT span is started (and put in the current context), so this
5554 // instrumentation runs again and the shouldStartSpan() check always returns true - and so on
@@ -62,18 +61,7 @@ private static JdbcAdviceScope start(
6261 }
6362
6463 Context parentContext = currentContext ();
65-
66- DbRequest request ;
67- if (sql != null ) {
68- request = DbRequest .create (statement , sql );
69- } else if (preparedStatement != null ) {
70- Map <String , String > parameters = JdbcData .getParameters (preparedStatement );
71- request = DbRequest .create (preparedStatement , parameters );
72- } else {
73- // batch
74- request = createBatchRequest (statement );
75- }
76-
64+ DbRequest request = requestSupplier .get ();
7765 if (request == null || !statementInstrumenter ().shouldStart (parentContext , request )) {
7866 return new JdbcAdviceScope (callDepth , null , null , null );
7967 }
0 commit comments