diff --git a/tdenginewriter/src/main/java/com/alibaba/datax/plugin/writer/JniConnection.java b/tdenginewriter/src/main/java/com/alibaba/datax/plugin/writer/JniConnection.java index 6c533ed8..5512aaf9 100644 --- a/tdenginewriter/src/main/java/com/alibaba/datax/plugin/writer/JniConnection.java +++ b/tdenginewriter/src/main/java/com/alibaba/datax/plugin/writer/JniConnection.java @@ -5,21 +5,22 @@ import java.util.Properties; public class JniConnection { private static final long JNI_NULL_POINTER = 0L; + private static final int JNI_SUCCESSFUL = 0; private static final String PROPERTY_KEY_CONFIG_DIR = "cfgdir"; private static final String PROPERTY_KEY_LOCALE = "locale"; private static final String PROPERTY_KEY_CHARSET = "charset"; private static final String PROPERTY_KEY_TIME_ZONE = "timezone"; - private long psql; + private long conn; static { System.loadLibrary("taos"); } public JniConnection(Properties props) { - if (this.psql != JNI_NULL_POINTER) { + if (this.conn != JNI_NULL_POINTER) { close(); - this.psql = JNI_NULL_POINTER; + this.conn = JNI_NULL_POINTER; } initImp(props.getProperty(PROPERTY_KEY_CONFIG_DIR, null)); @@ -39,31 +40,35 @@ public class JniConnection { } public void open(String host, int port, String dbname, String user, String password) { - if (this.psql != JNI_NULL_POINTER) { + if (this.conn != JNI_NULL_POINTER) { close(); - this.psql = JNI_NULL_POINTER; + this.conn = JNI_NULL_POINTER; } - this.psql = connectImp(host, port, dbname, user, password); - if (this.psql == JNI_NULL_POINTER) { + this.conn = connectImp(host, port, dbname, user, password); + if (this.conn == JNI_NULL_POINTER) { String errMsg = getErrMsgImp(0); throw new RuntimeException(errMsg); } } - public long insertOpentsdbJson(String json) { - if (this.psql == JNI_NULL_POINTER) { + public void insertOpentsdbJson(String json) { + if (this.conn == JNI_NULL_POINTER) { throw new RuntimeException("JNI connection is NULL"); } - return insertOpentsdbJson(json, this.psql); + long code = insertOpentsdbJson(json, this.conn); + if (code != JNI_SUCCESSFUL) { + String errMsg = getErrMsgByCode(code); + throw new RuntimeException(errMsg); + } } public void close() { - int code = this.closeConnectionImp(this.psql); + int code = this.closeConnectionImp(this.conn); if (code != 0) { throw new RuntimeException("JNI closeConnection failed"); } - this.psql = JNI_NULL_POINTER; + this.conn = JNI_NULL_POINTER; } private static native void initImp(String configDir); @@ -80,6 +85,8 @@ public class JniConnection { private native String getErrMsgImp(long pSql); + private native String getErrMsgByCode(long code); + private native int getAffectedRowsImp(long connection, long pSql); private native int closeConnectionImp(long connection); diff --git a/tdenginewriter/src/main/java/com/alibaba/datax/plugin/writer/com_alibaba_datax_plugin_writer_JniConnection.h b/tdenginewriter/src/main/java/com/alibaba/datax/plugin/writer/com_alibaba_datax_plugin_writer_JniConnection.h index 58cac666..4bdf3639 100644 --- a/tdenginewriter/src/main/java/com/alibaba/datax/plugin/writer/com_alibaba_datax_plugin_writer_JniConnection.h +++ b/tdenginewriter/src/main/java/com/alibaba/datax/plugin/writer/com_alibaba_datax_plugin_writer_JniConnection.h @@ -9,6 +9,8 @@ extern "C" { #endif #undef com_alibaba_datax_plugin_writer_JniConnection_JNI_NULL_POINTER #define com_alibaba_datax_plugin_writer_JniConnection_JNI_NULL_POINTER 0LL +#undef com_alibaba_datax_plugin_writer_JniConnection_JNI_SUCCESSFUL +#define com_alibaba_datax_plugin_writer_JniConnection_JNI_SUCCESSFUL 0L /* * Class: com_alibaba_datax_plugin_writer_JniConnection * Method: initImp @@ -65,6 +67,14 @@ JNIEXPORT jint JNICALL Java_com_alibaba_datax_plugin_writer_JniConnection_getErr JNIEXPORT jstring JNICALL Java_com_alibaba_datax_plugin_writer_JniConnection_getErrMsgImp (JNIEnv *, jobject, jlong); +/* + * Class: com_alibaba_datax_plugin_writer_JniConnection + * Method: getErrMsgByCode + * Signature: (J)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_com_alibaba_datax_plugin_writer_JniConnection_getErrMsgByCode + (JNIEnv *, jobject, jlong); + /* * Class: com_alibaba_datax_plugin_writer_JniConnection * Method: getAffectedRowsImp diff --git a/tdenginewriter/src/test/java/com/alibaba/datax/plugin/writer/JniConnectionTest.java b/tdenginewriter/src/test/java/com/alibaba/datax/plugin/writer/JniConnectionTest.java index 87fa668d..603931ef 100644 --- a/tdenginewriter/src/test/java/com/alibaba/datax/plugin/writer/JniConnectionTest.java +++ b/tdenginewriter/src/test/java/com/alibaba/datax/plugin/writer/JniConnectionTest.java @@ -10,7 +10,7 @@ public class JniConnectionTest { public void test() { JniConnection connection = new JniConnection(new Properties()); - connection.open("192.168.56.105", 6030, "log", "root", "taosdata"); + connection.open("192.168.56.105", 6030, "test", "root", "taosdata"); String json = "{\"metric\":\"weather.temperature\",\"timestamp\":1609430400000,\"value\":123,\"tags\":{\"location\":\"beijing\",\"id\":123}}"; connection.insertOpentsdbJson(json);