Skip to content

Commit f951cd3

Browse files
committed
bug fix
1 parent 6836b17 commit f951cd3

1 file changed

Lines changed: 61 additions & 46 deletions

File tree

jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -873,52 +873,6 @@ private InterpreterResult executeSql(String sql,
873873

874874
String interpreterName = getInterpreterGroup().getId();
875875

876-
String sqlToValidate = sql
877-
.replace("\n", " ")
878-
.replace("\r", " ")
879-
.replace("\t", " ");
880-
881-
// User config properties may be null until setUserProperty is called (e.g. first run for this user)
882-
Properties defaultProps = basePropertiesMap.get(DEFAULT_KEY);
883-
String targetJdbcUrl = (defaultProps != null ? defaultProps.getProperty(URL_KEY) : null);
884-
885-
ValidationRequest request = new ValidationRequest(sqlToValidate, user,
886-
interpreterName, sql, targetJdbcUrl);
887-
ValidationResponse response = null;
888-
889-
try {
890-
response = sendValidationRequest(request);
891-
892-
if (response.getNewJdbcUrl() != null &&
893-
!response.getNewJdbcUrl().isEmpty()) {
894-
targetJdbcUrl = response.getNewJdbcUrl();
895-
}
896-
} catch (Exception e) {
897-
LOGGER.warn("Failed to call validation API: {}", e);
898-
}
899-
900-
try {
901-
connection = getConnection(context, targetJdbcUrl);
902-
} catch (IllegalArgumentException e) {
903-
LOGGER.error("Cannot run " + sql, e);
904-
return new InterpreterResult(Code.ERROR, "Connection URL contains improper configuration");
905-
} catch (Exception e) {
906-
LOGGER.error("Fail to getConnection", e);
907-
try {
908-
closeDBPool(user);
909-
} catch (SQLException e1) {
910-
LOGGER.error("Cannot close DBPool for user: " + user , e1);
911-
}
912-
if (e instanceof SQLException) {
913-
return new InterpreterResult(Code.ERROR, e.getMessage());
914-
} else {
915-
return new InterpreterResult(Code.ERROR, ExceptionUtils.getStackTrace(e));
916-
}
917-
}
918-
if (connection == null) {
919-
return new InterpreterResult(Code.ERROR, "User's connection not found.");
920-
}
921-
922876
try {
923877
List<String> sqlArray = sqlSplitter.splitSql(sql);
924878
for (String sqlToExecute : sqlArray) {
@@ -932,6 +886,67 @@ private InterpreterResult executeSql(String sql,
932886
sqlToExecute = sqlToExecute.trim();
933887
}
934888
LOGGER.info("Execute sql: " + sqlToExecute);
889+
// Validate and get URL for THIS specific statement
890+
String sqlToValidate = sqlToExecute
891+
.replace("\n", " ")
892+
.replace("\r", " ")
893+
.replace("\t", " ");
894+
895+
// User config properties may be null until setUserProperty is called (e.g. first run for this user)
896+
Properties defaultProps = basePropertiesMap.get(DEFAULT_KEY);
897+
String targetJdbcUrl = (defaultProps != null ? defaultProps.getProperty(URL_KEY) : null);
898+
899+
ValidationRequest request = new ValidationRequest(sqlToValidate, user,
900+
interpreterName, sqlToExecute, targetJdbcUrl);
901+
ValidationResponse response = null;
902+
903+
try {
904+
response = sendValidationRequest(request);
905+
906+
if (response.getNewJdbcUrl() != null &&
907+
!response.getNewJdbcUrl().isEmpty()) {
908+
targetJdbcUrl = response.getNewJdbcUrl();
909+
LOGGER.info("Validation API returned new JDBC URL for statement");
910+
}
911+
} catch (Exception e) {
912+
LOGGER.warn("Failed to call validation API: {}", e.getMessage());
913+
}
914+
915+
// Get or create connection for this URL if needed
916+
try {
917+
// Close existing connection if URL changed
918+
if (connection != null && !connection.isClosed()) {
919+
String currentUrl = connection.getMetaData().getURL();
920+
if (targetJdbcUrl != null && !currentUrl.equals(targetJdbcUrl)) {
921+
LOGGER.info("URL changed, closing old connection");
922+
connection.close();
923+
connection = null;
924+
}
925+
}
926+
927+
if (connection == null || connection.isClosed()) {
928+
connection = getConnection(context, targetJdbcUrl);
929+
}
930+
} catch (IllegalArgumentException e) {
931+
LOGGER.error("Cannot run " + sqlToExecute, e);
932+
return new InterpreterResult(Code.ERROR, "Connection URL contains improper configuration");
933+
} catch (Exception e) {
934+
LOGGER.error("Fail to getConnection", e);
935+
try {
936+
closeDBPool(user);
937+
} catch (SQLException e1) {
938+
LOGGER.error("Cannot close DBPool for user: " + user , e1);
939+
}
940+
if (e instanceof SQLException) {
941+
return new InterpreterResult(Code.ERROR, e.getMessage());
942+
} else {
943+
return new InterpreterResult(Code.ERROR, ExceptionUtils.getStackTrace(e));
944+
}
945+
}
946+
947+
if (connection == null) {
948+
return new InterpreterResult(Code.ERROR, "User's connection not found.");
949+
}
935950
statement = connection.createStatement();
936951

937952
if (interpreterName != null && interpreterName.startsWith("spark_rca_")) {

0 commit comments

Comments
 (0)