Skip to content

[Bug]: MysqlSession Can Not Save New Sessions when auto-commit=false #1087

@hanydd

Description

@hanydd

AgentScope-Java is an open-source project. To involve a broader community, we recommend asking your questions in English.

Describe the bug
MysqlSession.save(SessionKey sessionKey, String key, List<? extends State> values) has inconsistent transaction behavior between the "full rewrite" branch and the "incremental append" branch.

full rewrite controls transaction commit manually:

if (needsFullRewrite) {
// Transaction: delete all + insert all
conn.setAutoCommit(false);
try {
deleteListItems(conn, sessionId, key);
insertAllItems(conn, sessionId, key, values);
saveHash(conn, sessionId, hashKey, currentHash);
conn.commit();
} catch (Exception e) {
conn.rollback();
throw e;
} finally {
conn.setAutoCommit(true);
}
} else if (values.size() > existingCount) {
// Incremental append
List<? extends State> newItems = values.subList(existingCount, values.size());
insertItems(conn, sessionId, key, newItems, existingCount);
saveHash(conn, sessionId, hashKey, currentHash);

whereas insert new items does not commit explicitly

private void insertItems(
Connection conn,
String sessionId,
String key,
List<? extends State> items,
int startIndex)
throws Exception {
String insertSql =
"INSERT INTO "
+ getFullTableName()
+ " (session_id, state_key, item_index, state_data)"
+ " VALUES (?, ?, ?, ?)";
try (PreparedStatement stmt = conn.prepareStatement(insertSql)) {
int index = startIndex;
for (State item : items) {
String json = JsonUtils.getJsonCodec().toJson(item);
stmt.setString(1, sessionId);
stmt.setString(2, key);
stmt.setInt(3, index);
stmt.setString(4, json);
stmt.addBatch();
index++;
}
stmt.executeBatch();
}
}

The use of transaction and auto-commit should be more consistent. And needsFullRewrite path changes the auto-commit behavior of the connection it gets.

To Reproduce
Steps to reproduce the behavior:

  1. Set auto-commit to false
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      auto-commit: false
  1. Save a new session
    MysqlSession saveSession = new MysqlSession()
    agent.saveTo(saveSession, sessionId);

  2. No new record is inserted

Environment (please complete the following information):

  • AgentScope-Java Version: 1.0.10
  • Java Version:17
  • OS: windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    In progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions