Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private void createTableIfNotExist() {
+ getFullTableName()
+ " (session_id VARCHAR(255) NOT NULL, state_key VARCHAR(255) NOT NULL,"
+ " item_index INT NOT NULL DEFAULT 0, state_data LONGTEXT NOT NULL,"
+ " created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP"
+ " created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME"
+ " DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY"
+ " (session_id, state_key, item_index)) DEFAULT CHARACTER SET utf8mb4"
+ " COLLATE utf8mb4_unicode_ci";
Expand Down Expand Up @@ -699,7 +699,9 @@ public DataSource getDataSource() {
* Clear all sessions from the database (for testing or cleanup).
*
* @return Number of rows deleted
* @deprecated Use {@link #truncateAllSessions()} instead
*/
@Deprecated
public int clearAllSessions() {
String clearSql = "DELETE FROM " + getFullTableName();

Expand All @@ -713,6 +715,31 @@ public int clearAllSessions() {
}
}

/**
* Truncate session table from the database (for testing or cleanup).
* <p>
* This method clears all session records by executing a TRUNCATE TABLE statement on the
* sessions table. TRUNCATE is faster than DELETE as it resets the table without logging
* individual row deletions and reclaims storage space immediately.
*
* <p>
* <strong>Note:</strong> The TRUNCATE operation requires DROP privileges in MySQL.
*
* @return typically 0 if successful
*/
public int truncateAllSessions() {
String clearSql = "TRUNCATE TABLE " + getFullTableName();

try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(clearSql)) {

return stmt.executeUpdate();

} catch (SQLException e) {
throw new RuntimeException("Failed to truncate sessions", e);
}
}

/**
* Validate a session ID format.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,18 @@ void testClearAllSessions() throws SQLException {
assertEquals(5, deleted);
}

@Test
@DisplayName("Should truncate session table")
void testTruncateAllSessions() throws SQLException {
when(mockStatement.execute()).thenReturn(true);
when(mockStatement.executeUpdate()).thenReturn(0);

MysqlSession session = new MysqlSession(mockDataSource, true);
int success = session.truncateAllSessions();

assertEquals(0, success);
}

@Test
@DisplayName("Should not close DataSource when closing session")
void testClose() throws SQLException {
Expand Down
Loading