From f5b889576503689937e6cb24140f1b1a421978eb Mon Sep 17 00:00:00 2001 From: liuhy Date: Sat, 25 Jul 2026 23:45:50 -0700 Subject: [PATCH] fix(h2): log SET SCHEMA failure instead of swallowing H2DBManager had an empty catch around SET SCHEMA execution, silently swallowing failures. Add @Slf4j and log.error, mirroring sibling DBManagers. Fixes #2149 Co-Authored-By: Claude --- .../ai/chat2db/plugin/h2/H2DBManager.java | 4 +- .../ai/chat2db/plugin/h2/H2DBManagerTest.java | 60 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 chat2db-community-server/chat2db-community-plugins/chat2db-community-h2/src/test/java/ai/chat2db/plugin/h2/H2DBManagerTest.java diff --git a/chat2db-community-server/chat2db-community-plugins/chat2db-community-h2/src/main/java/ai/chat2db/plugin/h2/H2DBManager.java b/chat2db-community-server/chat2db-community-plugins/chat2db-community-h2/src/main/java/ai/chat2db/plugin/h2/H2DBManager.java index dcb9f1b89d..1fc6959cdd 100644 --- a/chat2db-community-server/chat2db-community-plugins/chat2db-community-h2/src/main/java/ai/chat2db/plugin/h2/H2DBManager.java +++ b/chat2db-community-server/chat2db-community-plugins/chat2db-community-h2/src/main/java/ai/chat2db/plugin/h2/H2DBManager.java @@ -8,6 +8,7 @@ import ai.chat2db.spi.DefaultSQLExecutor; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; +import lombok.extern.slf4j.Slf4j; import java.sql.Connection; import java.sql.ResultSet; @@ -15,6 +16,7 @@ import java.sql.PreparedStatement; import static ai.chat2db.plugin.h2.constant.H2DBManagerConstants.*; +@Slf4j public class H2DBManager extends DefaultDBManager implements IDbManager { @@ -53,7 +55,7 @@ public void connectDatabase(Connection connection, String database) { try { DefaultSQLExecutor.getInstance().execute(connection, String.format(SQL_SET_SCHEMA, schemaName)); } catch (SQLException e) { - + log.error("Failed to set schema: {}", schemaName, e); } } diff --git a/chat2db-community-server/chat2db-community-plugins/chat2db-community-h2/src/test/java/ai/chat2db/plugin/h2/H2DBManagerTest.java b/chat2db-community-server/chat2db-community-plugins/chat2db-community-h2/src/test/java/ai/chat2db/plugin/h2/H2DBManagerTest.java new file mode 100644 index 0000000000..0ef000b335 --- /dev/null +++ b/chat2db-community-server/chat2db-community-plugins/chat2db-community-h2/src/test/java/ai/chat2db/plugin/h2/H2DBManagerTest.java @@ -0,0 +1,60 @@ +package ai.chat2db.plugin.h2; + +import ai.chat2db.community.domain.api.config.DriverConfig; +import ai.chat2db.spi.model.datasource.ConnectInfo; +import ai.chat2db.spi.sql.Chat2DBContext; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +/** + * Regression test for {@link H2DBManager#connectDatabase}. + * Verifies that a SQLException during SET SCHEMA is caught and logged + * instead of propagating to the caller. + */ +class H2DBManagerTest { + + private H2DBManager manager = new H2DBManager(); + + @AfterEach + void cleanup() { + Chat2DBContext.removeContext(); + } + + private void putH2Context(String schemaName) { + ConnectInfo connectInfo = new ConnectInfo(); + connectInfo.setDbType("H2"); + connectInfo.setSchemaName(schemaName); + DriverConfig driverConfig = new DriverConfig(); + driverConfig.setDbType("H2"); + connectInfo.setDriverConfig(driverConfig); + Chat2DBContext.putContext(connectInfo); + } + + @Test + void connectDatabaseCatchesSQLExceptionForBadSchema() throws SQLException { + // Set up context with a non-existent schema name + putH2Context("NONEXISTENT_SCHEMA_12345"); + + // Create an H2 in-memory connection + try (Connection connection = DriverManager.getConnection("jdbc:h2:mem:test_h2db_manager")) { + // connectDatabase should catch the SQLException from SET SCHEMA and not propagate it + assertDoesNotThrow(() -> manager.connectDatabase(connection, "testdb")); + } + } + + @Test + void connectDatabaseReturnsEarlyWhenSchemaNameIsBlank() throws SQLException { + // Set up context with a blank schema name — method should return early + putH2Context(""); + + try (Connection connection = DriverManager.getConnection("jdbc:h2:mem:test_h2db_manager_blank")) { + assertDoesNotThrow(() -> manager.connectDatabase(connection, "testdb")); + } + } +}