Skip to content

Commit added11

Browse files
committed
fix: wrong dependency for databasedriver and polish
Signed-off-by: Xavier Chopin <[email protected]>
1 parent be93230 commit added11

File tree

3 files changed

+12
-42
lines changed

3 files changed

+12
-42
lines changed

memory/spring-ai-model-chat-memory-jdbc/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
</dependency>
4747

4848
<dependency>
49-
<groupId>org.springframework</groupId>
50-
<artifactId>spring-jdbc</artifactId>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-data-jdbc</artifactId>
5151
</dependency>
5252

5353
<dependency>

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ SELECT TOP (?) content, type, [timestamp] \
6060
WHERE conversation_id = ? \
6161
ORDER BY [timestamp] DESC \
6262
) AS recent \
63-
ORDER BY [timestamp] ASC \
63+
ORDER BY [timestamp] ASC
6464
""";
6565

6666
private static final String QUERY_CLEAR = "DELETE FROM ai_chat_memory WHERE conversation_id = ?";
@@ -71,7 +71,7 @@ SELECT TOP (?) content, type, [timestamp] \
7171

7272
public JdbcChatMemory(JdbcChatMemoryConfig config) {
7373
this.jdbcTemplate = config.getJdbcTemplate();
74-
this.driver = this.detectDialect(this.jdbcTemplate);
74+
this.driver = this.detectDatabaseDriver(this.jdbcTemplate);
7575
}
7676

7777
public static JdbcChatMemory create(JdbcChatMemoryConfig config) {
@@ -130,15 +130,14 @@ public Message mapRow(ResultSet rs, int i) throws SQLException {
130130

131131
}
132132

133-
private DatabaseDriver detectDialect(JdbcTemplate jdbcTemplate) {
133+
private DatabaseDriver detectDatabaseDriver(JdbcTemplate jdbcTemplate) {
134+
Assert.notNull(jdbcTemplate.getDataSource(), "jdbcTemplate.dataSource must not be null");
134135
try {
135-
Assert.notNull(jdbcTemplate.getDataSource(), "jdbcTemplate.dataSource must not be null");
136-
try (Connection conn = jdbcTemplate.getDataSource().getConnection()) {
137-
String url = conn.getMetaData().getURL();
138-
return DatabaseDriver.fromJdbcUrl(url);
139-
}
136+
Connection conn = jdbcTemplate.getDataSource().getConnection();
137+
String url = conn.getMetaData().getURL();
138+
return DatabaseDriver.fromJdbcUrl(url);
140139
} catch (SQLException ex) {
141-
throw new IllegalStateException("Impossible to detect dialect", ex);
140+
throw new IllegalStateException("Impossible to detect the database driver", ex);
142141
}
143142
}
144143
}

memory/spring-ai-model-chat-memory-jdbc/src/test/java/org/springframework/ai/chat/memory/jdbc/JdbcChatMemoryMSSQLServerIT.java

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

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

19-
import org.junit.jupiter.api.BeforeAll;
2019
import org.junit.jupiter.api.Test;
2120
import org.junit.jupiter.params.ParameterizedTest;
2221
import org.junit.jupiter.params.provider.CsvSource;
@@ -36,11 +35,6 @@
3635
import org.testcontainers.junit.jupiter.Testcontainers;
3736

3837
import javax.sql.DataSource;
39-
import java.nio.file.Files;
40-
import java.nio.file.Path;
41-
import java.sql.Connection;
42-
import java.sql.DriverManager;
43-
import java.sql.Statement;
4438
import java.sql.Timestamp;
4539
import java.util.List;
4640
import java.util.UUID;
@@ -59,38 +53,15 @@ class JdbcChatMemoryMSSQLServerIT {
5953
.acceptLicense()
6054
.withEnv("MSSQL_PID", "Express")
6155
.withEnv("MSSQL_DATABASE", "chat_memory_test")
62-
.withPassword("Strong!NotR34LLyPassword");
56+
.withPassword("Strong!NotR34LLyPassword")
57+
.withInitScript("org/springframework/ai/chat/memory/jdbc/schema-mssql.sql");
6358

6459
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
6560
.withUserConfiguration(TestApplication.class)
6661
.withPropertyValues(String.format("app.datasource.url=%s", mssqlContainer.getJdbcUrl()),
6762
String.format("app.datasource.username=%s", mssqlContainer.getUsername()),
6863
String.format("app.datasource.password=%s", mssqlContainer.getPassword()));
6964

70-
@BeforeAll
71-
static void initializeSchema() throws Exception {
72-
String jdbcUrl = mssqlContainer.getJdbcUrl();
73-
String username = mssqlContainer.getUsername();
74-
String password = mssqlContainer.getPassword();
75-
76-
String sql = Files.readString(Path.of(
77-
JdbcChatMemoryMSSQLServerIT.class
78-
.getClassLoader()
79-
.getResource("org/springframework/ai/chat/memory/jdbc/schema-mssql.sql")
80-
.toURI()
81-
));
82-
83-
try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password);
84-
Statement stmt = conn.createStatement()) {
85-
86-
for (String statement : sql.split(";")) {
87-
String trimmed = statement.trim();
88-
if (!trimmed.isEmpty()) {
89-
stmt.execute(trimmed);
90-
}
91-
}
92-
}
93-
}
9465
@Test
9566
void correctChatMemoryInstance() {
9667
this.contextRunner.run(context -> {

0 commit comments

Comments
 (0)