Skip to content

Commit 7b6ac15

Browse files
sunyuhan1998sobychacko
authored andcommitted
GH-4289: Optimized JdbcChatMemoryRepositoryDialect#from
* 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. * 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; Fixes #4289 Signed-off-by: Sun Yuhan <[email protected]>
1 parent 3f97d9e commit 7b6ac15

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
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: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,22 @@
1616

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

19+
import java.sql.DatabaseMetaData;
20+
1921
import javax.sql.DataSource;
20-
import java.sql.Connection;
22+
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
import org.springframework.jdbc.support.JdbcUtils;
2127

2228
/**
2329
* Abstraction for database-specific SQL for chat memory repository.
2430
*/
2531
public interface JdbcChatMemoryRepositoryDialect {
2632

33+
Logger logger = LoggerFactory.getLogger(JdbcChatMemoryRepositoryDialect.class);
34+
2735
/**
2836
* Returns the SQL to fetch messages for a conversation, ordered by timestamp, with
2937
* limit.
@@ -50,32 +58,29 @@ public interface JdbcChatMemoryRepositoryDialect {
5058
*/
5159

5260
/**
53-
* Detects the dialect from the DataSource or JDBC URL.
61+
* Detects the dialect from the DataSource.
5462
*/
5563
static JdbcChatMemoryRepositoryDialect from(DataSource dataSource) {
56-
// Simple detection (could be improved)
57-
try (Connection connection = dataSource.getConnection()) {
58-
String url = connection.getMetaData().getURL().toLowerCase();
59-
if (url.contains("postgresql")) {
60-
return new PostgresChatMemoryRepositoryDialect();
61-
}
62-
if (url.contains("mysql")) {
63-
return new MysqlChatMemoryRepositoryDialect();
64-
}
65-
if (url.contains("mariadb")) {
66-
return new MysqlChatMemoryRepositoryDialect();
67-
}
68-
if (url.contains("sqlserver")) {
69-
return new SqlServerChatMemoryRepositoryDialect();
70-
}
71-
if (url.contains("hsqldb")) {
72-
return new HsqldbChatMemoryRepositoryDialect();
73-
}
74-
// Add more as needed
64+
String productName = null;
65+
try {
66+
productName = JdbcUtils.extractDatabaseMetaData(dataSource, DatabaseMetaData::getDatabaseProductName);
67+
}
68+
catch (Exception e) {
69+
logger.warn("Due to failure in establishing JDBC connection or parsing metadata, the JDBC database vendor "
70+
+ "could not be determined", e);
7571
}
76-
catch (Exception ignored) {
72+
if (productName == null || productName.trim().isEmpty()) {
73+
logger.warn("Database product name is null or empty, defaulting to Postgres dialect.");
74+
return new PostgresChatMemoryRepositoryDialect();
7775
}
78-
return new PostgresChatMemoryRepositoryDialect(); // default
76+
return switch (productName) {
77+
case "PostgreSQL" -> new PostgresChatMemoryRepositoryDialect();
78+
case "MySQL", "MariaDB" -> new MysqlChatMemoryRepositoryDialect();
79+
case "Microsoft SQL Server" -> new SqlServerChatMemoryRepositoryDialect();
80+
case "HSQL Database Engine" -> new HsqldbChatMemoryRepositoryDialect();
81+
default -> // Add more as needed
82+
new PostgresChatMemoryRepositoryDialect();
83+
};
7984
}
8085

8186
}

0 commit comments

Comments
 (0)