16
16
17
17
package org .springframework .ai .chat .memory .repository .jdbc ;
18
18
19
+ import java .sql .Timestamp ;
20
+ import java .util .List ;
21
+ import java .util .UUID ;
22
+ import javax .sql .DataSource ;
23
+
19
24
import org .junit .jupiter .api .Test ;
20
25
import org .junit .jupiter .params .ParameterizedTest ;
21
26
import org .junit .jupiter .params .provider .CsvSource ;
27
+
22
28
import org .springframework .ai .chat .memory .ChatMemoryRepository ;
29
+ import org .springframework .ai .chat .messages .AssistantMessage ;
23
30
import org .springframework .ai .chat .messages .Message ;
24
31
import org .springframework .ai .chat .messages .MessageType ;
25
- import org .springframework .ai .chat .messages .AssistantMessage ;
26
32
import org .springframework .ai .chat .messages .SystemMessage ;
27
33
import org .springframework .ai .chat .messages .UserMessage ;
28
34
import org .springframework .beans .factory .annotation .Autowired ;
33
39
import org .springframework .boot .test .context .SpringBootTest ;
34
40
import org .springframework .context .annotation .Bean ;
35
41
import org .springframework .jdbc .core .JdbcTemplate ;
42
+ import org .springframework .jdbc .datasource .DataSourceTransactionManager ;
36
43
import org .springframework .test .context .TestPropertySource ;
37
44
import org .springframework .test .context .jdbc .Sql ;
38
-
39
- import java .sql .Timestamp ;
40
- import java .util .List ;
41
- import java .util .UUID ;
42
-
43
- import javax .sql .DataSource ;
45
+ import org .springframework .transaction .support .TransactionTemplate ;
44
46
45
47
import static org .assertj .core .api .Assertions .assertThat ;
46
48
@@ -156,6 +158,34 @@ void deleteMessagesByConversationId() {
156
158
assertThat (count ).isZero ();
157
159
}
158
160
161
+ @ Test
162
+ void repositoryWithExplicitTransactionManager () {
163
+ // Get the repository with explicit transaction manager
164
+ ChatMemoryRepository repositoryWithTxManager = TestConfiguration
165
+ .chatMemoryRepositoryWithTransactionManager (jdbcTemplate , jdbcTemplate .getDataSource ());
166
+
167
+ var conversationId = UUID .randomUUID ().toString ();
168
+ var messages = List .<Message >of (new AssistantMessage ("Message with transaction manager - " + conversationId ),
169
+ new UserMessage ("User message with transaction manager - " + conversationId ));
170
+
171
+ // Save messages using the repository with explicit transaction manager
172
+ repositoryWithTxManager .saveAll (conversationId , messages );
173
+
174
+ // Verify messages were saved correctly
175
+ var savedMessages = repositoryWithTxManager .findByConversationId (conversationId );
176
+ assertThat (savedMessages ).hasSize (2 );
177
+ assertThat (savedMessages ).isEqualTo (messages );
178
+
179
+ // Verify transaction works by updating and checking atomicity
180
+ var newMessages = List .<Message >of (new SystemMessage ("New system message - " + conversationId ));
181
+ repositoryWithTxManager .saveAll (conversationId , newMessages );
182
+
183
+ // The old messages should be deleted and only the new one should exist
184
+ var updatedMessages = repositoryWithTxManager .findByConversationId (conversationId );
185
+ assertThat (updatedMessages ).hasSize (1 );
186
+ assertThat (updatedMessages ).isEqualTo (newMessages );
187
+ }
188
+
159
189
@ SpringBootConfiguration
160
190
@ ImportAutoConfiguration ({ DataSourceAutoConfiguration .class , JdbcTemplateAutoConfiguration .class })
161
191
static class TestConfiguration {
@@ -168,6 +198,20 @@ ChatMemoryRepository chatMemoryRepository(JdbcTemplate jdbcTemplate, DataSource
168
198
.build ();
169
199
}
170
200
201
+ @ Bean
202
+ ChatMemoryRepository chatMemoryRepositoryWithTxManager (JdbcTemplate jdbcTemplate , DataSource dataSource ) {
203
+ return chatMemoryRepositoryWithTransactionManager (jdbcTemplate , dataSource );
204
+ }
205
+
206
+ static ChatMemoryRepository chatMemoryRepositoryWithTransactionManager (JdbcTemplate jdbcTemplate ,
207
+ DataSource dataSource ) {
208
+ return JdbcChatMemoryRepository .builder ()
209
+ .jdbcTemplate (jdbcTemplate )
210
+ .dialect (JdbcChatMemoryRepositoryDialect .from (dataSource ))
211
+ .transactionManager (new DataSourceTransactionManager (dataSource ))
212
+ .build ();
213
+ }
214
+
171
215
}
172
216
173
217
}
0 commit comments