Skip to content

Commit 27cf57b

Browse files
cxzl25pan3793
authored andcommitted
[KYUUBI #2666] Backport HIVE-24694 to Kyuubi Hive JDBC
### _Why are the changes needed?_ HIVE-[24694](https://issues.apache.org/jira/browse/HIVE-24694): Early connection close to release server resources during creating close #2666 ### _How was this patch tested?_ - [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible - [ ] Add screenshots for manual tests if appropriate - [ ] [Run test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request Closes #2667 from cxzl25/KYUUBI-2666. Closes #2666 01d33a5 [sychen] Backport HIVE-24694 Authored-by: sychen <sychen@ctrip.com> Signed-off-by: Cheng Pan <chengpan@apache.org>
1 parent a163f3a commit 27cf57b

File tree

1 file changed

+57
-20
lines changed

1 file changed

+57
-20
lines changed

kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiConnection.java

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ public KyuubiConnection(String uri, Properties info) throws SQLException {
204204
LOG.warn("Failed to connect to " + connParams.getHost() + ":" + connParams.getPort());
205205
String errMsg = null;
206206
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+
}
207213
if (ZooKeeperHiveClientHelper.isZkDynamicDiscoveryMode(sessConfMap)) {
208214
errMsg = "Could not open client transport for any of the Server URI's in ZooKeeper: ";
209215
// Try next available server in zookeeper, or retry all the servers again if retry is
@@ -314,13 +320,15 @@ public void executeInitSql() throws SQLException {
314320
if (initFile != null) {
315321
try {
316322
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+
}
324332
}
325333
}
326334
}
@@ -910,6 +918,9 @@ public void abort(Executor executor) throws SQLException {
910918
}
911919

912920
public String getDelegationToken(String owner, String renewer) throws SQLException {
921+
if (isClosed) {
922+
throw new SQLException("Connection is closed");
923+
}
913924
TGetDelegationTokenReq req = new TGetDelegationTokenReq(sessHandle, owner, renewer);
914925
try {
915926
TGetDelegationTokenResp tokenResp = client.GetDelegationToken(req);
@@ -921,6 +932,9 @@ public String getDelegationToken(String owner, String renewer) throws SQLExcepti
921932
}
922933

923934
public void cancelDelegationToken(String tokenStr) throws SQLException {
935+
if (isClosed) {
936+
throw new SQLException("Connection is closed");
937+
}
924938
TCancelDelegationTokenReq cancelReq = new TCancelDelegationTokenReq(sessHandle, tokenStr);
925939
try {
926940
TCancelDelegationTokenResp cancelResp = client.CancelDelegationToken(cancelReq);
@@ -932,6 +946,9 @@ public void cancelDelegationToken(String tokenStr) throws SQLException {
932946
}
933947

934948
public void renewDelegationToken(String tokenStr) throws SQLException {
949+
if (isClosed) {
950+
throw new SQLException("Connection is closed");
951+
}
935952
TRenewDelegationTokenReq cancelReq = new TRenewDelegationTokenReq(sessHandle, tokenStr);
936953
try {
937954
TRenewDelegationTokenResp renewResp = client.RenewDelegationToken(cancelReq);
@@ -961,17 +978,19 @@ public void clearWarnings() throws SQLException {
961978

962979
@Override
963980
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);
967984
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;
975994
}
976995
}
977996
}
@@ -1094,6 +1113,9 @@ public Statement createStatement(int resultSetType, int resultSetConcurrency)
10941113
"Statement with resultset type " + resultSetType + " is not supported",
10951114
"HYC00"); // Optional feature not implemented
10961115
}
1116+
if (isClosed) {
1117+
throw new SQLException("Connection is closed");
1118+
}
10971119
return new KyuubiStatement(
10981120
this, client, sessHandle, resultSetType == ResultSet.TYPE_SCROLL_INSENSITIVE, fetchSize);
10991121
}
@@ -1279,6 +1301,9 @@ public boolean isValid(int timeout) throws SQLException {
12791301
if (timeout < 0) {
12801302
throw new SQLException("timeout value was negative");
12811303
}
1304+
if (isClosed) {
1305+
return false;
1306+
}
12821307
boolean rc = false;
12831308
try {
12841309
String productName =
@@ -1349,6 +1374,9 @@ public CallableStatement prepareCall(
13491374

13501375
@Override
13511376
public PreparedStatement prepareStatement(String sql) throws SQLException {
1377+
if (isClosed) {
1378+
throw new SQLException("Connection is closed");
1379+
}
13521380
return new KyuubiPreparedStatement(this, client, sessHandle, sql);
13531381
}
13541382

@@ -1360,6 +1388,9 @@ public PreparedStatement prepareStatement(String sql) throws SQLException {
13601388

13611389
@Override
13621390
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
1391+
if (isClosed) {
1392+
throw new SQLException("Connection is closed");
1393+
}
13631394
return new KyuubiPreparedStatement(this, client, sessHandle, sql);
13641395
}
13651396

@@ -1397,6 +1428,9 @@ public PreparedStatement prepareStatement(String sql, String[] columnNames) thro
13971428
@Override
13981429
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
13991430
throws SQLException {
1431+
if (isClosed) {
1432+
throw new SQLException("Connection is closed");
1433+
}
14001434
return new KyuubiPreparedStatement(this, client, sessHandle, sql);
14011435
}
14021436

@@ -1516,6 +1550,9 @@ public void setClientInfo(String name, String value) throws SQLClientInfoExcepti
15161550
}
15171551

15181552
private void setClientInfo() throws SQLClientInfoException {
1553+
if (isClosed) {
1554+
throw new SQLClientInfoException("Connection is closed", null);
1555+
}
15191556
TSetClientInfoReq req = new TSetClientInfoReq(sessHandle);
15201557
Map<String, String> map = new HashMap<>();
15211558
if (clientInfo != null) {
@@ -1604,9 +1641,9 @@ public void setSchema(String schema) throws SQLException {
16041641
if (schema == null || schema.isEmpty()) {
16051642
throw new SQLException("Schema name is null or empty");
16061643
}
1607-
Statement stmt = createStatement();
1608-
stmt.execute("use " + schema);
1609-
stmt.close();
1644+
try (Statement stmt = createStatement()) {
1645+
stmt.execute("use " + schema);
1646+
}
16101647
}
16111648

16121649
/*

0 commit comments

Comments
 (0)