@@ -939,19 +939,18 @@ public static void executeNonQueriesWithRetry(
939939 }
940940 }
941941
942- public static boolean tryExecuteNonQueryWithRetry (
943- BaseEnv env , String sql , Connection defaultConnection ) {
944- return tryExecuteNonQueryWithRetry (
942+ public static boolean executeNonQuery (BaseEnv env , String sql , Connection defaultConnection ) {
943+ return executeNonQuery (
945944 env , sql , SessionConfig .DEFAULT_USER , SessionConfig .DEFAULT_PASSWORD , defaultConnection );
946945 }
947946
948- public static boolean tryExecuteNonQueryWithRetry (
947+ public static boolean executeNonQuery (
949948 String dataBaseName ,
950949 String sqlDialect ,
951950 BaseEnv env ,
952951 String sql ,
953952 Connection defaultConnection ) {
954- return tryExecuteNonQueryWithRetry (
953+ return executeNonQuery (
955954 env ,
956955 sql ,
957956 SessionConfig .DEFAULT_USER ,
@@ -961,21 +960,21 @@ public static boolean tryExecuteNonQueryWithRetry(
961960 defaultConnection );
962961 }
963962
964- public static boolean tryExecuteNonQueryWithRetry (
963+ public static boolean executeNonQuery (
965964 BaseEnv env , String sql , String userName , String password , Connection defaultConnection ) {
966- return tryExecuteNonQueriesWithRetry (
965+ return executeNonQueries (
967966 env , Collections .singletonList (sql ), userName , password , defaultConnection );
968967 }
969968
970- public static boolean tryExecuteNonQueryWithRetry (
969+ public static boolean executeNonQuery (
971970 BaseEnv env ,
972971 String sql ,
973972 String userName ,
974973 String password ,
975974 String dataBaseName ,
976975 String sqlDialect ,
977976 Connection defaultConnection ) {
978- return tryExecuteNonQueriesWithRetry (
977+ return executeNonQueries (
979978 env ,
980979 Collections .singletonList (sql ),
981980 userName ,
@@ -985,9 +984,9 @@ public static boolean tryExecuteNonQueryWithRetry(
985984 defaultConnection );
986985 }
987986
988- public static boolean tryExecuteNonQueriesWithRetry (
987+ public static boolean executeNonQueries (
989988 BaseEnv env , List <String > sqlList , Connection defaultConnection ) {
990- return tryExecuteNonQueriesWithRetry (
989+ return executeNonQueries (
991990 env ,
992991 sqlList ,
993992 SessionConfig .DEFAULT_USER ,
@@ -997,13 +996,13 @@ public static boolean tryExecuteNonQueriesWithRetry(
997996 defaultConnection );
998997 }
999998
1000- public static boolean tryExecuteNonQueriesWithRetry (
999+ public static boolean executeNonQueries (
10011000 String dataBase ,
10021001 String sqlDialect ,
10031002 BaseEnv env ,
10041003 List <String > sqlList ,
10051004 Connection defaultConnection ) {
1006- return tryExecuteNonQueriesWithRetry (
1005+ return executeNonQueries (
10071006 env ,
10081007 sqlList ,
10091008 SessionConfig .DEFAULT_USER ,
@@ -1015,16 +1014,62 @@ public static boolean tryExecuteNonQueriesWithRetry(
10151014
10161015 // This method will not throw failure given that a failure is encountered.
10171016 // Instead, it returns a flag to indicate the result of the execution.
1018- public static boolean tryExecuteNonQueriesWithRetry (
1017+ public static boolean executeNonQueries (
10191018 BaseEnv env ,
10201019 List <String > sqlList ,
10211020 String userName ,
10221021 String password ,
10231022 Connection defaultConnection ) {
1024- return tryExecuteNonQueriesWithRetry (
1023+ return executeNonQueries (
10251024 env , sqlList , userName , password , null , TREE_SQL_DIALECT , defaultConnection );
10261025 }
10271026
1027+ public static boolean executeNonQueries (
1028+ BaseEnv env ,
1029+ List <String > sqlList ,
1030+ String userName ,
1031+ String password ,
1032+ String dataBase ,
1033+ String sqlDialect ,
1034+ Connection defaultConnection ) {
1035+ int lastIndex = 0 ;
1036+ Connection localConnection = null ;
1037+ Connection connectionToUse = defaultConnection ;
1038+ Statement statement ;
1039+ try {
1040+ // create a new connection if default is not provided or the previous is broken
1041+ if (connectionToUse == null ) {
1042+ localConnection =
1043+ env .getConnection (
1044+ userName ,
1045+ password ,
1046+ BaseEnv .TABLE_SQL_DIALECT .equals (sqlDialect )
1047+ ? BaseEnv .TABLE_SQL_DIALECT
1048+ : TREE_SQL_DIALECT );
1049+ connectionToUse = localConnection ;
1050+ }
1051+ statement = connectionToUse .createStatement ();
1052+ if (BaseEnv .TABLE_SQL_DIALECT .equals (sqlDialect ) && dataBase != null ) {
1053+ statement .execute ("use " + dataBase );
1054+ }
1055+ for (int i = lastIndex ; i < sqlList .size (); ++i ) {
1056+ statement .execute (sqlList .get (i ));
1057+ }
1058+ return true ;
1059+ } catch (SQLException e ) {
1060+ // the default connection should be closed by the upper level
1061+ // while the local connection should be closed here
1062+ if (connectionToUse == localConnection && localConnection != null ) {
1063+ try {
1064+ localConnection .close ();
1065+ } catch (SQLException ex ) {
1066+ // ignore
1067+ }
1068+ }
1069+ throw new RuntimeException (e );
1070+ }
1071+ }
1072+
10281073 public static boolean tryExecuteNonQueriesWithRetry (
10291074 BaseEnv env ,
10301075 List <String > sqlList ,
@@ -1073,7 +1118,16 @@ public static boolean tryExecuteNonQueriesWithRetry(
10731118 }
10741119 }
10751120 connectionToUse = null ;
1076- throw new RuntimeException (e );
1121+
1122+ if (retryCountLeft > 0 ) {
1123+ try {
1124+ Thread .sleep (10000 );
1125+ } catch (InterruptedException ignored ) {
1126+ }
1127+ } else {
1128+ e .printStackTrace ();
1129+ return false ;
1130+ }
10771131 }
10781132 }
10791133 return false ;
0 commit comments