kitoha opened a new pull request, #8210:
URL: https://github.com/apache/gravitino/pull/8210
## Title: [#8165] [Fix] If commit and rollback fails, session close and
threadLocal remove.
### What changes were proposed in this pull request?
- Sessions are properly closed even when commit/rollback operations throw
exceptions
- Thread-local variables are cleared to prevent memory leaks
### Why are the changes needed?
- Memory leaks: Thread-local variables not being cleared
- Connection pool exhaustion: Unclosed connections consuming pool resources
change code
```java
private static void executeAndCleanupSession(Consumer<SqlSession> consumer)
{
SqlSession sqlSession = sessions.get();
if (sqlSession == null) {
getSessions().remove();
return;
}
try (sqlSession) {
consumer.accept(sqlSession);
} finally {
sessions.remove();
}
}
```
Fix: #8165
### Does this PR introduce _any_ user-facing change?
### How was this patch tested?
[Commit Test]
https://github.com/kitoha/gravitino/commit/5e90ddece13c43a71c323556f710dffe832492aa#diff-d1475432e21dd81c01f472b229c2049423bad8a5231cbd46afa37286c27ea54aR157
```java
@Test
public void testCommitAndCloseSqlSessionWhenCommitFails() {
SqlSession mockSession = Mockito.mock(SqlSession.class);
Mockito.doThrow(new RuntimeException("commit
fail")).when(mockSession).commit();
SqlSessions.getSessions().set(mockSession);
assertThrows(RuntimeException.class,
SqlSessions::commitAndCloseSqlSession);
Mockito.verify(mockSession).close();
assertNull(SqlSessions.getSessions().get());
}
```
[Rollback
Test](https://github.com/kitoha/gravitino/commit/5e90ddece13c43a71c323556f710dffe832492aa#diff-d1475432e21dd81c01f472b229c2049423bad8a5231cbd46afa37286c27ea54aR167)
```java
@Test
public void testRollbackAndCloseSqlSessionWhenRollbackFails() {
SqlSession mockSession = Mockito.mock(SqlSession.class);
Mockito.doThrow(new RuntimeException("rollback
fail")).when(mockSession).rollback();
SqlSessions.getSessions().set(mockSession);
assertThrows(RuntimeException.class,
SqlSessions::rollbackAndCloseSqlSession);
Mockito.verify(mockSession).close();
assertNull(SqlSessions.getSessions().get());
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]