1616
1717package org .springframework .ai .chat .memory .repository .jdbc ;
1818
19+ import java .sql .Timestamp ;
20+ import java .util .List ;
21+ import java .util .UUID ;
22+ import javax .sql .DataSource ;
23+
1924import org .junit .jupiter .api .Test ;
2025import org .junit .jupiter .params .ParameterizedTest ;
2126import org .junit .jupiter .params .provider .CsvSource ;
27+
2228import org .springframework .ai .chat .memory .ChatMemoryRepository ;
29+ import org .springframework .ai .chat .messages .AssistantMessage ;
2330import org .springframework .ai .chat .messages .Message ;
2431import org .springframework .ai .chat .messages .MessageType ;
25- import org .springframework .ai .chat .messages .AssistantMessage ;
2632import org .springframework .ai .chat .messages .SystemMessage ;
2733import org .springframework .ai .chat .messages .UserMessage ;
2834import org .springframework .beans .factory .annotation .Autowired ;
3339import org .springframework .boot .test .context .SpringBootTest ;
3440import org .springframework .context .annotation .Bean ;
3541import org .springframework .jdbc .core .JdbcTemplate ;
42+ import org .springframework .jdbc .datasource .DataSourceTransactionManager ;
3643import org .springframework .test .context .TestPropertySource ;
3744import 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 ;
4446
4547import static org .assertj .core .api .Assertions .assertThat ;
4648
@@ -156,6 +158,34 @@ void deleteMessagesByConversationId() {
156158 assertThat (count ).isZero ();
157159 }
158160
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+
159189 @ SpringBootConfiguration
160190 @ ImportAutoConfiguration ({ DataSourceAutoConfiguration .class , JdbcTemplateAutoConfiguration .class })
161191 static class TestConfiguration {
@@ -168,6 +198,20 @@ ChatMemoryRepository chatMemoryRepository(JdbcTemplate jdbcTemplate, DataSource
168198 .build ();
169199 }
170200
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+
171215 }
172216
173217}
0 commit comments