Skip to content

Commit d4d5c60

Browse files
committed
polish: GH-4289 Optimized JdbcChatMemoryRepositoryDialect#from
1. Rewrote the method to use `org.springframework.jdbc.support.JdbcUtils#extractDatabaseMetaData` for extracting database metadata from the `dataSource`, and obtain the database vendor name from the JDBC driver, improving accuracy and robustness; 2. Enhanced exception handling: instead of silently ignoring exceptions, it now explicitly reports encountered issues, helping users identify problems and select the appropriate dialect more easily; Signed-off-by: Sun Yuhan <[email protected]>
1 parent 1dd686b commit d4d5c60

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryDialect.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
package org.springframework.ai.chat.memory.repository.jdbc;
1818

19+
import org.springframework.jdbc.support.JdbcUtils;
20+
import org.springframework.jdbc.support.MetaDataAccessException;
21+
1922
import java.sql.Connection;
23+
import java.sql.DatabaseMetaData;
2024

2125
import javax.sql.DataSource;
2226

@@ -51,32 +55,24 @@ public interface JdbcChatMemoryRepositoryDialect {
5155
*/
5256

5357
/**
54-
* Detects the dialect from the DataSource or JDBC URL.
58+
* Detects the dialect from the DataSource.
5559
*/
5660
static JdbcChatMemoryRepositoryDialect from(DataSource dataSource) {
57-
// Simple detection (could be improved)
58-
try (Connection connection = dataSource.getConnection()) {
59-
String url = connection.getMetaData().getURL().toLowerCase();
60-
if (url.contains("postgresql")) {
61-
return new PostgresChatMemoryRepositoryDialect();
62-
}
63-
if (url.contains("mysql")) {
64-
return new MysqlChatMemoryRepositoryDialect();
65-
}
66-
if (url.contains("mariadb")) {
67-
return new MysqlChatMemoryRepositoryDialect();
68-
}
69-
if (url.contains("sqlserver")) {
70-
return new SqlServerChatMemoryRepositoryDialect();
71-
}
72-
if (url.contains("hsqldb")) {
73-
return new HsqldbChatMemoryRepositoryDialect();
74-
}
75-
// Add more as needed
61+
String productName;
62+
try {
63+
productName = JdbcUtils.extractDatabaseMetaData(dataSource, DatabaseMetaData::getDatabaseProductName);
7664
}
77-
catch (Exception ignored) {
65+
catch (Exception e) {
66+
throw new RuntimeException("Failed to obtain JDBC product name or establish JDBC connection", e);
7867
}
79-
return new PostgresChatMemoryRepositoryDialect(); // default
68+
return switch (productName) {
69+
case "PostgreSQL" -> new PostgresChatMemoryRepositoryDialect();
70+
case "MySQL", "MariaDB" -> new MysqlChatMemoryRepositoryDialect();
71+
case "Microsoft SQL Server" -> new SqlServerChatMemoryRepositoryDialect();
72+
case "HSQL Database Engine" -> new HsqldbChatMemoryRepositoryDialect();
73+
default -> // Add more as needed
74+
new PostgresChatMemoryRepositoryDialect();
75+
};
8076
}
8177

8278
}

0 commit comments

Comments
 (0)