未验证 提交 62f76881 编写于 作者: P Patrick Jiang(白泽) 提交者: GitHub

Fix issue 4965 (#5109)

上级 8b46e5c5
......@@ -40,15 +40,15 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho
public final void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, MethodInterceptResult result) {
StatementEnhanceInfos cacheObject = (StatementEnhanceInfos) objInst.getSkyWalkingDynamicField();
ConnectionInfo connectInfo = cacheObject.getConnectionInfo();
/**
* For avoid NPE. In this particular case, Execute sql inside the {@link com.mysql.jdbc.ConnectionImpl} constructor,
* before the interceptor sets the connectionInfo.
*
* When invoking prepareCall, cacheObject is null. Because it will determine procedures's parameter types by executing sql in mysql
* before the interceptor sets the statementEnhanceInfos.
* @see JDBCDriverInterceptor#afterMethod(EnhancedInstance, Method, Object[], Class[], Object)
*/
if (connectInfo != null) {
if (cacheObject != null && cacheObject.getConnectionInfo() != null) {
ConnectionInfo connectInfo = cacheObject.getConnectionInfo();
AbstractSpan span = ContextManager.createExitSpan(buildOperationName(connectInfo, method.getName(), cacheObject
.getStatementName()), connectInfo.getDatabasePeer());
Tags.DB_TYPE.set(span, "sql");
......@@ -73,7 +73,7 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho
public final Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Object ret) {
StatementEnhanceInfos cacheObject = (StatementEnhanceInfos) objInst.getSkyWalkingDynamicField();
if (cacheObject.getConnectionInfo() != null) {
if (cacheObject != null && cacheObject.getConnectionInfo() != null) {
ContextManager.stopSpan();
}
return ret;
......@@ -83,7 +83,7 @@ public class PreparedStatementExecuteMethodsInterceptor implements InstanceMetho
public final void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
StatementEnhanceInfos cacheObject = (StatementEnhanceInfos) objInst.getSkyWalkingDynamicField();
if (cacheObject.getConnectionInfo() != null) {
if (cacheObject != null && cacheObject.getConnectionInfo() != null) {
ContextManager.activeSpan().errorOccurred().log(t);
}
}
......
......@@ -72,10 +72,77 @@ segmentItems:
componentId: 33
peer: mysql-server:3306
skipAnalysis: 'false'
- operationName: Mysql/JDBI/Connection/close
- operationName: Mysql/JDBI/Statement/execute
operationId: eq 0
parentSpanId: 0
spanId: 4
tags:
- {key: db.type, value: sql}
- {key: db.instance, value: test}
- {key: db.statement, value: "create procedure testProcedure(IN id varchar(10)) \n begin \n select id; \n end"}
logs: []
startTime: nq 0
endTime: nq 0
isError: false
spanLayer: Database
spanType: Exit
componentId: 33
peer: mysql-server:3306
skipAnalysis: 'false'
- operationName: Mysql/JDBI/Statement/executeQuery
operationId: eq 0
parentSpanId: 0
spanId: 5
spanLayer: Database
startTime: nq 0
endTime: nq 0
componentId: 33
isError: false
spanType: Exit
peer: mysql-server:3306
skipAnalysis: false
tags:
- {key: db.type, value: sql}
- {key: db.instance, value: test}
- {key: db.statement, value: SHOW CREATE PROCEDURE `test`.`testProcedure`}
- operationName: Mysql/JDBI/CallableStatement/execute
operationId: eq 0
parentSpanId: 0
spanId: 6
tags:
- {key: db.type, value: sql}
- {key: db.instance, value: test}
- {key: db.statement, value: "call testProcedure( ? )"}
logs: []
startTime: nq 0
endTime: nq 0
isError: false
spanLayer: Database
spanType: Exit
componentId: 33
peer: mysql-server:3306
skipAnalysis: 'false'
- operationName: Mysql/JDBI/Statement/execute
operationId: eq 0
parentSpanId: 0
spanId: 7
tags:
- {key: db.type, value: sql}
- {key: db.instance, value: test}
- {key: db.statement, value: "drop procedure testProcedure"}
logs: []
startTime: nq 0
endTime: nq 0
isError: false
spanLayer: Database
spanType: Exit
componentId: 33
peer: mysql-server:3306
skipAnalysis: 'false'
- operationName: Mysql/JDBI/Connection/close
operationId: eq 0
parentSpanId: 0
spanId: 8
tags:
- {key: db.type, value: sql}
- {key: db.instance, value: test}
......
......@@ -51,11 +51,30 @@ public class SQLExecutor implements AutoCloseable {
}
public void dropTable(String sql) throws SQLException {
executeStatement(sql);
}
public void createProcedure(String sql) throws SQLException {
executeStatement(sql);
}
public void dropProcedure(String sql) throws SQLException {
executeStatement(sql);
}
public void callProcedure(String sql, String id) throws SQLException {
PreparedStatement preparedStatement = connection.prepareCall(sql);
preparedStatement.setString(1, id);
preparedStatement.execute();
preparedStatement.close();
}
public void executeStatement(String sql) throws SQLException {
Statement preparedStatement = connection.createStatement();
preparedStatement.execute(sql);
preparedStatement.close();
}
public void closeConnection() throws SQLException {
if (this.connection != null) {
this.connection.close();
......
......@@ -40,7 +40,10 @@ public class CaseController {
private static final String QUERY_DATA_SQL = "SELECT id, value FROM test_007 WHERE id=?";
private static final String DELETE_DATA_SQL = "DELETE FROM test_007 WHERE id=?";
private static final String DROP_TABLE_SQL = "DROP table test_007";
private static final String CREATE_PROCEDURE_SQL = "create procedure testProcedure(IN id varchar(10)) \n begin \n select id; \n end";
private static final String CALL_PROCEDURE_SQL = "call testProcedure( ? )";
private static final String DROP_PROCEDURE_SQL = "drop procedure testProcedure";
@RequestMapping("/mysql-scenario")
@ResponseBody
public String testcase() throws Exception {
......@@ -48,6 +51,9 @@ public class CaseController {
sqlExecute.createTable(CREATE_TABLE_SQL);
sqlExecute.insertData(INSERT_DATA_SQL, "1", "1");
sqlExecute.dropTable(DROP_TABLE_SQL);
sqlExecute.createProcedure(CREATE_PROCEDURE_SQL);
sqlExecute.callProcedure(CALL_PROCEDURE_SQL, "nihao");
sqlExecute.dropProcedure(DROP_PROCEDURE_SQL);
} catch (Exception e) {
logger.error("Failed to execute sql.", e);
throw e;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册