Skip to content

Commit 0a78b48

Browse files
committed
fix: Adjust the transaction implementation method in JdbcChatMemoryRepository.
Signed-off-by: Sun Yuhan <[email protected]>
1 parent 418171e commit 0a78b48

File tree

1 file changed

+15
-23
lines changed
  • memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc

1 file changed

+15
-23
lines changed

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

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.time.Instant;
2121
import java.util.ArrayList;
2222
import java.util.List;
23-
import java.util.Optional;
2423
import java.util.concurrent.atomic.AtomicLong;
2524

2625
import org.springframework.ai.chat.memory.ChatMemoryRepository;
@@ -33,8 +32,9 @@
3332
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
3433
import org.springframework.jdbc.core.JdbcTemplate;
3534
import org.springframework.jdbc.core.RowMapper;
36-
import org.springframework.jdbc.datasource.DataSourceUtils;
35+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
3736
import org.springframework.lang.Nullable;
37+
import org.springframework.transaction.support.TransactionTemplate;
3838
import org.springframework.util.Assert;
3939

4040
/**
@@ -83,30 +83,22 @@ public void saveAll(String conversationId, List<Message> messages) {
8383
Assert.notNull(messages, "messages cannot be null");
8484
Assert.noNullElements(messages, "messages cannot contain null elements");
8585

86-
Connection connection = null;
87-
Assert.notNull(jdbcTemplate.getDataSource(), "jdbcTemplate.getDataSource() cannot be null");
88-
try {
89-
connection = DataSourceUtils.getConnection(jdbcTemplate.getDataSource());
90-
connection.setAutoCommit(false);
91-
this.deleteByConversationId(conversationId);
92-
this.jdbcTemplate.batchUpdate(dialect.getInsertMessageSql(),
93-
new AddBatchPreparedStatement(conversationId, messages));
94-
connection.commit();
95-
}
96-
catch (SQLException ex) {
86+
Assert.notNull(jdbcTemplate.getDataSource(), "dataSource can not be null");
87+
TransactionTemplate transactionTemplate = new TransactionTemplate(
88+
new DataSourceTransactionManager(jdbcTemplate.getDataSource()));
89+
90+
transactionTemplate.execute(status -> {
9791
try {
98-
connection.rollback();
92+
deleteByConversationId(conversationId);
93+
jdbcTemplate.batchUpdate(dialect.getInsertMessageSql(),
94+
new AddBatchPreparedStatement(conversationId, messages));
9995
}
100-
catch (SQLException e) {
101-
throw new RuntimeException("Transaction rollback exception", e);
96+
catch (RuntimeException e) {
97+
status.setRollbackOnly();
98+
throw e;
10299
}
103-
throw new RuntimeException("save messages failed", ex);
104-
}
105-
finally {
106-
Optional.ofNullable(connection)
107-
.ifPresent(conn -> DataSourceUtils.releaseConnection(conn, jdbcTemplate.getDataSource()));
108-
}
109-
100+
return null;
101+
});
110102
}
111103

112104
@Override

0 commit comments

Comments
 (0)