This is an automated email from the ASF dual-hosted git repository.

jt2594838 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new e09005e87d0 Preserve JDBC session diagnostics across locales (#19102)
e09005e87d0 is described below

commit e09005e87d036d308676a2c7a879898a9f231329
Author: Caideyipi <[email protected]>
AuthorDate: Tue Nov 3 17:18:23 2026 +0800

    Preserve JDBC session diagnostics across locales (#19102)
    
    * Preserve JDBC legacy-server diagnostics across locales
    
    * Handle JDBC session diagnostics across locales and absent messages
    
    * Declare logback-core for JDBC test dependencies
    
    * Test JDBC diagnostics without logging dependencies
---
 .../org/apache/iotdb/jdbc/IoTDBConnection.java     |  53 +++++---
 .../org/apache/iotdb/jdbc/IoTDBConnectionTest.java | 150 +++++++++++++++++++++
 2 files changed, 184 insertions(+), 19 deletions(-)

diff --git 
a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java 
b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java
index d81f15acdf6..96beecffe1f 100644
--- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java
+++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java
@@ -73,6 +73,13 @@ public class IoTDBConnection implements Connection {
   private static final Logger logger = 
LoggerFactory.getLogger(IoTDBConnection.class);
   private static final TSProtocolVersion protocolVersion =
       TSProtocolVersion.IOTDB_SERVICE_PROTOCOL_V3;
+  // Match Thrift's fixed protocol text independently of the localized 
SQLException message.
+  private static final String MISSING_CLIENT_PROTOCOL_DIAGNOSTIC =
+      "Required field 'client_protocol' was not present!";
+  // Recognize server diagnostics in either supported language, regardless of 
the driver's locale.
+  private static final String[] PASSWORD_EXPIRATION_DIAGNOSTIC_PREFIXES = {
+    "Your password will expire at ", "密码将于以下时间过期:"
+  };
   private static final String NOT_SUPPORT_PREPARE_CALL =
       JdbcMessages.EXCEPTION_DOES_NOT_SUPPORT_PREPARECALL_2E958060;
   private static final String NOT_SUPPORT_PREPARE_STATEMENT =
@@ -570,6 +577,22 @@ public class IoTDBConnection implements Connection {
     }
   }
 
+  static String getPasswordExpirationDateToWarn(String message, LocalDateTime 
now) {
+    if (message == null) {
+      return null;
+    }
+    for (String expirationPrefix : PASSWORD_EXPIRATION_DIAGNOSTIC_PREFIXES) {
+      int expirationIndex = message.indexOf(expirationPrefix);
+      if (expirationIndex != -1) {
+        String expirationDateStr = message.substring(expirationIndex + 
expirationPrefix.length());
+        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd 
HH:mm:ss");
+        LocalDateTime expirationDate = 
LocalDateTime.from(dateFormat.parse(expirationDateStr));
+        return now.isAfter(expirationDate.minusDays(3)) ? expirationDateStr : 
null;
+      }
+    }
+    return null;
+  }
+
   private void openSession() throws SQLException {
     TSOpenSessionReq openReq = new TSOpenSessionReq();
 
@@ -602,40 +625,32 @@ public class IoTDBConnection implements Connection {
                   openResp.getServerProtocolVersion().getValue()));
         }
       }
-      String expirationInformer = 
JdbcMessages.EXCEPTION_YOUR_PASSWORD_WILL_EXPIRE_AT_49098220;
-      String message = openResp.getStatus().getMessage();
-      int expirationIndex = message.indexOf(expirationInformer);
-      if (expirationIndex != -1) {
-        String expirationDateStr = message.substring(expirationIndex + 
expirationInformer.length());
-        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd 
HH:mm:ss");
-        LocalDateTime expirationDate = 
LocalDateTime.from(dateFormat.parse(expirationDateStr));
-        LocalDateTime now = LocalDateTime.now();
-        if (now.isAfter(expirationDate.minusDays(3))) {
-          logger.warn(
-              
JdbcMessages.LOG_ARG_ARG_PLEASE_CHANGE_IT_TIME_VIA_ALTER_USER_STATEMENT_6B67087C,
-              expirationInformer,
-              expirationDateStr);
-        }
+      String expirationDateStr =
+          getPasswordExpirationDateToWarn(openResp.getStatus().getMessage(), 
LocalDateTime.now());
+      if (expirationDateStr != null) {
+        logger.warn(
+            
JdbcMessages.LOG_ARG_ARG_PLEASE_CHANGE_IT_TIME_VIA_ALTER_USER_STATEMENT_6B67087C,
+            JdbcMessages.EXCEPTION_YOUR_PASSWORD_WILL_EXPIRE_AT_49098220,
+            expirationDateStr);
       }
     } catch (TException e) {
       transport.close();
-      if (e.getMessage()
-          .contains(
-              
JdbcMessages.EXCEPTION_REQUIRED_FIELD_CLIENT_PROTOCOL_WAS_NOT_PRESENT_2059BC1E))
 {
+      String message = e.getMessage();
+      if (message != null && 
message.contains(MISSING_CLIENT_PROTOCOL_DIAGNOSTIC)) {
         // the server is an old version (less than 0.10)
         throw new SQLException(
             String.format(
                 JdbcMessages
                     
.EXCEPTION_CAN_NOT_ESTABLISH_CONNECTION_ARG_YOU_MAY_TRY_CONNECT_OLD_8FC3703E,
                 params.getJdbcUriString(),
-                e.getMessage()),
+                message),
             e);
       }
       throw new SQLException(
           String.format(
               
JdbcMessages.EXCEPTION_CAN_NOT_ESTABLISH_CONNECTION_ARG_ARG_D7246055,
               params.getJdbcUriString(),
-              e.getMessage()),
+              message),
           e);
     } catch (StatementExecutionException e) {
       // failed to connect, disconnect from the server
diff --git 
a/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBConnectionTest.java
 
b/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBConnectionTest.java
index be3c7de929e..ea22d4c2fb7 100644
--- 
a/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBConnectionTest.java
+++ 
b/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBConnectionTest.java
@@ -20,13 +20,19 @@
 package org.apache.iotdb.jdbc;
 
 import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.jdbc.i18n.JdbcMessages;
 import org.apache.iotdb.rpc.RpcUtils;
 import org.apache.iotdb.service.rpc.thrift.IClientRPCService;
 import org.apache.iotdb.service.rpc.thrift.ServerProperties;
 import org.apache.iotdb.service.rpc.thrift.TSGetTimeZoneResp;
+import org.apache.iotdb.service.rpc.thrift.TSOpenSessionReq;
+import org.apache.iotdb.service.rpc.thrift.TSOpenSessionResp;
+import org.apache.iotdb.service.rpc.thrift.TSProtocolVersion;
 import org.apache.iotdb.service.rpc.thrift.TSSetTimeZoneReq;
 
 import org.apache.thrift.TException;
+import org.apache.thrift.transport.TTransport;
+import org.apache.thrift.transport.TTransportException;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
@@ -34,14 +40,24 @@ import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.sql.SQLClientInfoException;
 import java.sql.SQLException;
+import java.time.LocalDateTime;
 import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Properties;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.doReturn;
@@ -53,6 +69,7 @@ import static org.mockito.Mockito.when;
 public class IoTDBConnectionTest {
 
   @Mock private IClientRPCService.Iface client;
+  @Mock private TTransport transport;
 
   private IoTDBConnection connection = new IoTDBConnection();
   private TSStatus successStatus = RpcUtils.SUCCESS_STATUS;
@@ -158,4 +175,137 @@ public class IoTDBConnectionTest {
 
     verify(tableConnection, never()).prepareStatement(anyString());
   }
+
+  @Test
+  public void testOpenSessionRecognizesLegacyProtocolDiagnostic() throws 
Exception {
+    TException cause =
+        new TException(
+            "Required field 'client_protocol' was not present! Struct: 
TSOpenSessionReq(...)");
+
+    SQLException exception = failOpenSession(cause);
+
+    assertEquals(
+        String.format(
+            JdbcMessages
+                
.EXCEPTION_CAN_NOT_ESTABLISH_CONNECTION_ARG_YOU_MAY_TRY_CONNECT_OLD_8FC3703E,
+            connection.getParams().getJdbcUriString(),
+            cause.getMessage()),
+        exception.getMessage());
+  }
+
+  @Test
+  public void testOpenSessionPreservesOtherMissingFieldDiagnostic() throws 
Exception {
+    TException cause =
+        new TException("Required field 'username' was not present! Struct: 
TSOpenSessionReq(...)");
+
+    SQLException exception = failOpenSession(cause);
+
+    assertEquals(
+        String.format(
+            
JdbcMessages.EXCEPTION_CAN_NOT_ESTABLISH_CONNECTION_ARG_ARG_D7246055,
+            connection.getParams().getJdbcUriString(),
+            cause.getMessage()),
+        exception.getMessage());
+  }
+
+  @Test
+  public void testOpenSessionPreservesMessageLessTransportException() throws 
Exception {
+    TException cause = new 
TTransportException(TTransportException.END_OF_FILE);
+
+    SQLException exception = failOpenSession(cause);
+
+    assertEquals(
+        String.format(
+            
JdbcMessages.EXCEPTION_CAN_NOT_ESTABLISH_CONNECTION_ARG_ARG_D7246055,
+            connection.getParams().getJdbcUriString(),
+            cause.getMessage()),
+        exception.getMessage());
+  }
+
+  @Test
+  public void testPasswordExpirationWarningDateForEnglishDiagnostic() {
+    String expirationDate = passwordExpirationDate(1);
+    assertEquals(
+        expirationDate,
+        IoTDBConnection.getPasswordExpirationDateToWarn(
+            "Login successfully. Your password will expire at " + 
expirationDate,
+            LocalDateTime.now()));
+  }
+
+  @Test
+  public void testPasswordExpirationWarningDateForChineseDiagnostic() {
+    String expirationDate = passwordExpirationDate(1);
+    assertEquals(
+        expirationDate,
+        IoTDBConnection.getPasswordExpirationDateToWarn(
+            "密码将于以下时间过期:" + expirationDate, LocalDateTime.now()));
+  }
+
+  @Test
+  public void testPasswordExpirationWarningDateIsAbsentForDistantExpiration() {
+    String expirationDate = passwordExpirationDate(30);
+    assertNull(
+        IoTDBConnection.getPasswordExpirationDateToWarn(
+            "Your password will expire at " + expirationDate, 
LocalDateTime.now()));
+  }
+
+  @Test
+  public void testPasswordExpirationWarningDateIsAbsentWithoutDiagnostic() {
+    assertNull(
+        IoTDBConnection.getPasswordExpirationDateToWarn("Login successfully", 
LocalDateTime.now()));
+  }
+
+  @Test
+  public void testOpenSessionWithoutStatusMessage() throws Exception {
+    assertOpenSessionSucceeds(null);
+  }
+
+  private String passwordExpirationDate(int daysUntilExpiration) {
+    return LocalDateTime.now()
+        .plusDays(daysUntilExpiration)
+        .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+  }
+
+  private void assertOpenSessionSucceeds(String statusMessage) throws 
Exception {
+    Method openSession = prepareOpenSession();
+    TSOpenSessionResp response =
+        new TSOpenSessionResp(
+                new TSStatus(successStatus).setMessage(statusMessage),
+                TSProtocolVersion.IOTDB_SERVICE_PROTOCOL_V3)
+            .setSessionId(1L);
+    when(client.openSession(any(TSOpenSessionReq.class))).thenReturn(response);
+    openSession.invoke(connection);
+
+    Assert.assertFalse(connection.isClosed());
+    assertEquals(1L, connection.getSessionId());
+    verify(transport, never()).close();
+  }
+
+  private Method prepareOpenSession() throws Exception {
+    IoTDBConnectionParams params = 
Utils.parseUrl("jdbc:iotdb://127.0.0.1:6667/", new Properties());
+    Field paramsField = IoTDBConnection.class.getDeclaredField("params");
+    paramsField.setAccessible(true);
+    paramsField.set(connection, params);
+    Field transportField = IoTDBConnection.class.getDeclaredField("transport");
+    transportField.setAccessible(true);
+    transportField.set(connection, transport);
+    connection.setClient(client);
+    Method openSession = 
IoTDBConnection.class.getDeclaredMethod("openSession");
+    openSession.setAccessible(true);
+    return openSession;
+  }
+
+  private SQLException failOpenSession(TException cause) throws Exception {
+    Method openSession = prepareOpenSession();
+    when(client.openSession(any(TSOpenSessionReq.class))).thenThrow(cause);
+
+    InvocationTargetException invocationException =
+        assertThrows(InvocationTargetException.class, () -> 
openSession.invoke(connection));
+
+    assertTrue(invocationException.getCause() instanceof SQLException);
+    SQLException exception = (SQLException) invocationException.getCause();
+    assertSame(cause, exception.getCause());
+    verify(transport).close();
+    return exception;
+  }
 }

Reply via email to