@@ -204,6 +204,12 @@ public KyuubiConnection(String uri, Properties info) throws SQLException {
204
204
LOG .warn ("Failed to connect to " + connParams .getHost () + ":" + connParams .getPort ());
205
205
String errMsg = null ;
206
206
String warnMsg = "Could not open client transport with JDBC Uri: " + jdbcUriString + ": " ;
207
+ try {
208
+ close ();
209
+ } catch (Exception ex ) {
210
+ // Swallow the exception
211
+ LOG .debug ("Error while closing the connection" , ex );
212
+ }
207
213
if (ZooKeeperHiveClientHelper .isZkDynamicDiscoveryMode (sessConfMap )) {
208
214
errMsg = "Could not open client transport for any of the Server URI's in ZooKeeper: " ;
209
215
// Try next available server in zookeeper, or retry all the servers again if retry is
@@ -314,13 +320,15 @@ public void executeInitSql() throws SQLException {
314
320
if (initFile != null ) {
315
321
try {
316
322
List <String > sqlList = parseInitFile (initFile );
317
- Statement st = createStatement ();
318
- for (String sql : sqlList ) {
319
- boolean hasResult = st .execute (sql );
320
- if (hasResult ) {
321
- ResultSet rs = st .getResultSet ();
322
- while (rs .next ()) {
323
- System .out .println (rs .getString (1 ));
323
+ try (Statement st = createStatement ()) {
324
+ for (String sql : sqlList ) {
325
+ boolean hasResult = st .execute (sql );
326
+ if (hasResult ) {
327
+ try (ResultSet rs = st .getResultSet ()) {
328
+ while (rs .next ()) {
329
+ System .out .println (rs .getString (1 ));
330
+ }
331
+ }
324
332
}
325
333
}
326
334
}
@@ -910,6 +918,9 @@ public void abort(Executor executor) throws SQLException {
910
918
}
911
919
912
920
public String getDelegationToken (String owner , String renewer ) throws SQLException {
921
+ if (isClosed ) {
922
+ throw new SQLException ("Connection is closed" );
923
+ }
913
924
TGetDelegationTokenReq req = new TGetDelegationTokenReq (sessHandle , owner , renewer );
914
925
try {
915
926
TGetDelegationTokenResp tokenResp = client .GetDelegationToken (req );
@@ -921,6 +932,9 @@ public String getDelegationToken(String owner, String renewer) throws SQLExcepti
921
932
}
922
933
923
934
public void cancelDelegationToken (String tokenStr ) throws SQLException {
935
+ if (isClosed ) {
936
+ throw new SQLException ("Connection is closed" );
937
+ }
924
938
TCancelDelegationTokenReq cancelReq = new TCancelDelegationTokenReq (sessHandle , tokenStr );
925
939
try {
926
940
TCancelDelegationTokenResp cancelResp = client .CancelDelegationToken (cancelReq );
@@ -932,6 +946,9 @@ public void cancelDelegationToken(String tokenStr) throws SQLException {
932
946
}
933
947
934
948
public void renewDelegationToken (String tokenStr ) throws SQLException {
949
+ if (isClosed ) {
950
+ throw new SQLException ("Connection is closed" );
951
+ }
935
952
TRenewDelegationTokenReq cancelReq = new TRenewDelegationTokenReq (sessHandle , tokenStr );
936
953
try {
937
954
TRenewDelegationTokenResp renewResp = client .RenewDelegationToken (cancelReq );
@@ -961,17 +978,19 @@ public void clearWarnings() throws SQLException {
961
978
962
979
@ Override
963
980
public void close () throws SQLException {
964
- if (! isClosed ) {
965
- TCloseSessionReq closeReq = new TCloseSessionReq ( sessHandle );
966
- try {
981
+ try {
982
+ if (! isClosed ) {
983
+ TCloseSessionReq closeReq = new TCloseSessionReq ( sessHandle );
967
984
client .CloseSession (closeReq );
968
- } catch (TException e ) {
969
- throw new SQLException ("Error while cleaning up the server resources" , e );
970
- } finally {
971
- isClosed = true ;
972
- if (transport != null ) {
973
- transport .close ();
974
- }
985
+ }
986
+ } catch (TException e ) {
987
+ throw new SQLException ("Error while cleaning up the server resources" , e );
988
+ } finally {
989
+ isClosed = true ;
990
+ client = null ;
991
+ if (transport != null && transport .isOpen ()) {
992
+ transport .close ();
993
+ transport = null ;
975
994
}
976
995
}
977
996
}
@@ -1094,6 +1113,9 @@ public Statement createStatement(int resultSetType, int resultSetConcurrency)
1094
1113
"Statement with resultset type " + resultSetType + " is not supported" ,
1095
1114
"HYC00" ); // Optional feature not implemented
1096
1115
}
1116
+ if (isClosed ) {
1117
+ throw new SQLException ("Connection is closed" );
1118
+ }
1097
1119
return new KyuubiStatement (
1098
1120
this , client , sessHandle , resultSetType == ResultSet .TYPE_SCROLL_INSENSITIVE , fetchSize );
1099
1121
}
@@ -1279,6 +1301,9 @@ public boolean isValid(int timeout) throws SQLException {
1279
1301
if (timeout < 0 ) {
1280
1302
throw new SQLException ("timeout value was negative" );
1281
1303
}
1304
+ if (isClosed ) {
1305
+ return false ;
1306
+ }
1282
1307
boolean rc = false ;
1283
1308
try {
1284
1309
String productName =
@@ -1349,6 +1374,9 @@ public CallableStatement prepareCall(
1349
1374
1350
1375
@ Override
1351
1376
public PreparedStatement prepareStatement (String sql ) throws SQLException {
1377
+ if (isClosed ) {
1378
+ throw new SQLException ("Connection is closed" );
1379
+ }
1352
1380
return new KyuubiPreparedStatement (this , client , sessHandle , sql );
1353
1381
}
1354
1382
@@ -1360,6 +1388,9 @@ public PreparedStatement prepareStatement(String sql) throws SQLException {
1360
1388
1361
1389
@ Override
1362
1390
public PreparedStatement prepareStatement (String sql , int autoGeneratedKeys ) throws SQLException {
1391
+ if (isClosed ) {
1392
+ throw new SQLException ("Connection is closed" );
1393
+ }
1363
1394
return new KyuubiPreparedStatement (this , client , sessHandle , sql );
1364
1395
}
1365
1396
@@ -1397,6 +1428,9 @@ public PreparedStatement prepareStatement(String sql, String[] columnNames) thro
1397
1428
@ Override
1398
1429
public PreparedStatement prepareStatement (String sql , int resultSetType , int resultSetConcurrency )
1399
1430
throws SQLException {
1431
+ if (isClosed ) {
1432
+ throw new SQLException ("Connection is closed" );
1433
+ }
1400
1434
return new KyuubiPreparedStatement (this , client , sessHandle , sql );
1401
1435
}
1402
1436
@@ -1516,6 +1550,9 @@ public void setClientInfo(String name, String value) throws SQLClientInfoExcepti
1516
1550
}
1517
1551
1518
1552
private void setClientInfo () throws SQLClientInfoException {
1553
+ if (isClosed ) {
1554
+ throw new SQLClientInfoException ("Connection is closed" , null );
1555
+ }
1519
1556
TSetClientInfoReq req = new TSetClientInfoReq (sessHandle );
1520
1557
Map <String , String > map = new HashMap <>();
1521
1558
if (clientInfo != null ) {
@@ -1604,9 +1641,9 @@ public void setSchema(String schema) throws SQLException {
1604
1641
if (schema == null || schema .isEmpty ()) {
1605
1642
throw new SQLException ("Schema name is null or empty" );
1606
1643
}
1607
- Statement stmt = createStatement ();
1608
- stmt .execute ("use " + schema );
1609
- stmt . close ();
1644
+ try ( Statement stmt = createStatement ()) {
1645
+ stmt .execute ("use " + schema );
1646
+ }
1610
1647
}
1611
1648
1612
1649
/*
0 commit comments