未验证 提交 197e59d8 编写于 作者: S Shengliang Guan 提交者: GitHub

Merge pull request #3152 from taosdata/hotfix/jdbcmemleak

fix jdbc memleaks  #3098
...@@ -327,13 +327,12 @@ JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getResultSetImp( ...@@ -327,13 +327,12 @@ JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getResultSetImp(
STscObj *pObj = pSql->pTscObj; STscObj *pObj = pSql->pTscObj;
if (tscIsUpdateQuery(pSql)) { if (tscIsUpdateQuery(pSql)) {
// taos_free_result(pSql); // free result here jniDebug("jobj:%p, conn:%p, update query, no resultset, %p", jobj, pObj, (void *)tres);
jniDebug("jobj:%p, conn:%p, no resultset, %p", jobj, pObj, (void *)tres);
return 0;
} else { } else {
jniDebug("jobj:%p, conn:%p, get resultset, %p", jobj, pObj, (void *)tres); jniDebug("jobj:%p, conn:%p, get resultset, %p", jobj, pObj, (void *)tres);
return tres;
} }
return tres;
} }
JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_freeResultSetImp(JNIEnv *env, jobject jobj, jlong con, JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_freeResultSetImp(JNIEnv *env, jobject jobj, jlong con,
......
...@@ -111,6 +111,8 @@ public class TSDBJNIConnector { ...@@ -111,6 +111,8 @@ public class TSDBJNIConnector {
* @throws SQLException * @throws SQLException
*/ */
public long executeQuery(String sql) throws SQLException { public long executeQuery(String sql) throws SQLException {
// close previous result set if the user forgets to invoke the
// free method to close previous result set.
if (!this.isResultsetClosed) { if (!this.isResultsetClosed) {
freeResultSet(taosResultSetPointer); freeResultSet(taosResultSetPointer);
} }
...@@ -123,21 +125,20 @@ public class TSDBJNIConnector { ...@@ -123,21 +125,20 @@ public class TSDBJNIConnector {
this.freeResultSet(pSql); this.freeResultSet(pSql);
throw new SQLException(TSDBConstants.WrapErrMsg("Unsupported encoding")); throw new SQLException(TSDBConstants.WrapErrMsg("Unsupported encoding"));
} }
int code = this.getErrCode(pSql); int code = this.getErrCode(pSql);
if (code != 0) { if (code != 0) {
affectedRows = -1; affectedRows = -1;
String err_msg = this.getErrMsg(pSql); String msg = this.getErrMsg(pSql);
this.freeResultSet(pSql); this.freeResultSet(pSql);
throw new SQLException(TSDBConstants.WrapErrMsg(err_msg), "", code); throw new SQLException(TSDBConstants.WrapErrMsg(msg), "", code);
} }
// Try retrieving result set for the executed SQL using the current connection pointer. If the executed // Try retrieving result set for the executed SQL using the current connection pointer.
// SQL is a DML/DDL which doesn't return a result set, then taosResultSetPointer should be 0L. Otherwise,
// taosResultSetPointer should be a non-zero value.
taosResultSetPointer = this.getResultSetImp(this.taos, pSql); taosResultSetPointer = this.getResultSetImp(this.taos, pSql);
if (taosResultSetPointer != TSDBConstants.JNI_NULL_POINTER) { isResultsetClosed = (taosResultSetPointer == TSDBConstants.JNI_NULL_POINTER);
isResultsetClosed = false;
}
return pSql; return pSql;
} }
...@@ -178,13 +179,14 @@ public class TSDBJNIConnector { ...@@ -178,13 +179,14 @@ public class TSDBJNIConnector {
int res = TSDBConstants.JNI_SUCCESS; int res = TSDBConstants.JNI_SUCCESS;
if (result != taosResultSetPointer && taosResultSetPointer != TSDBConstants.JNI_NULL_POINTER) { if (result != taosResultSetPointer && taosResultSetPointer != TSDBConstants.JNI_NULL_POINTER) {
throw new RuntimeException("Invalid result set pointer"); throw new RuntimeException("Invalid result set pointer");
} else if (taosResultSetPointer != TSDBConstants.JNI_NULL_POINTER) { }
if (taosResultSetPointer != TSDBConstants.JNI_NULL_POINTER) {
res = this.freeResultSetImp(this.taos, result); res = this.freeResultSetImp(this.taos, result);
isResultsetClosed = true; // reset resultSetPointer to 0 after freeResultSetImp() return
taosResultSetPointer = TSDBConstants.JNI_NULL_POINTER; taosResultSetPointer = TSDBConstants.JNI_NULL_POINTER;
} else {
isResultsetClosed = true;
} }
isResultsetClosed = true;
return res; return res;
} }
......
...@@ -51,6 +51,8 @@ public class TSDBStatement implements Statement { ...@@ -51,6 +51,8 @@ public class TSDBStatement implements Statement {
if (isClosed) { if (isClosed) {
throw new SQLException("Invalid method call on a closed statement."); throw new SQLException("Invalid method call on a closed statement.");
} }
// TODO make sure it is not a update query
pSql = this.connecter.executeQuery(sql); pSql = this.connecter.executeQuery(sql);
long resultSetPointer = this.connecter.getResultSet(); long resultSetPointer = this.connecter.getResultSet();
...@@ -71,20 +73,19 @@ public class TSDBStatement implements Statement { ...@@ -71,20 +73,19 @@ public class TSDBStatement implements Statement {
if (isClosed) { if (isClosed) {
throw new SQLException("Invalid method call on a closed statement."); throw new SQLException("Invalid method call on a closed statement.");
} }
// TODO check if current query is update query
pSql = this.connecter.executeQuery(sql); pSql = this.connecter.executeQuery(sql);
long resultSetPointer = this.connecter.getResultSet(); long resultSetPointer = this.connecter.getResultSet();
if (resultSetPointer == TSDBConstants.JNI_CONNECTION_NULL) { if (resultSetPointer == TSDBConstants.JNI_CONNECTION_NULL) {
this.connecter.freeResultSet(pSql); this.connecter.freeResultSet(pSql);
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL)); throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
} else if (resultSetPointer != TSDBConstants.JNI_NULL_POINTER) { }
this.connecter.freeResultSet();
throw new SQLException("The executed SQL is not a DML or a DDL"); int num = this.connecter.getAffectedRows(pSql);
} else { this.connecter.freeResultSet(pSql);
int num = this.connecter.getAffectedRows(pSql); return num;
this.connecter.freeResultSet(pSql);
return num;
}
} }
public String getErrorMsg(long pSql) { public String getErrorMsg(long pSql) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册