World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#10177] improvement(core): ensure SessionUtils rolls back on Throwable (#10335)
ChangJun Rhoad54a7a2a7b4
diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/utils/SessionUtils.java b/core/src/main/java/org/apache/gravitino/storage/relational/utils/SessionUtils.javaindex 0482bfecfd..8a36bc1add 100644--- a/core/src/main/java/org/apache/gravitino/storage/relational/utils/SessionUtils.java+++ b/core/src/main/java/org/apache/gravitino/storage/relational/utils/SessionUtils.java@@ -40,9 +40,9 @@ public class SessionUtils {T mapper = SqlSessions.getMapper(mapperClazz);consumer.accept(mapper);SqlSessions.commitAndCloseSqlSession();- } catch (Exception e) {+ } catch (Throwable t) {SqlSessions.rollbackAndCloseSqlSession();- throw e;+ throw t;}}@@ -56,9 +56,9 @@ public class SessionUtils {R result = func.apply(mapper);SqlSessions.commitAndCloseSqlSession();return result;- } catch (Exception e) {+ } catch (Throwable t) {SqlSessions.rollbackAndCloseSqlSession();- throw e;+ throw t;}}@@ -101,9 +101,9 @@ public class SessionUtils {try {Arrays.stream(operations).forEach(Runnable::run);SqlSessions.commitAndCloseSqlSession();- } catch (Exception e) {+ } catch (Throwable t) {SqlSessions.rollbackAndCloseSqlSession();- throw e;+ throw t;}}diff --git a/core/src/test/java/org/apache/gravitino/storage/relational/utils/TestSessionUtils.java b/core/src/test/java/org/apache/gravitino/storage/relational/utils/TestSessionUtils.javanew file mode 100644index 0000000000..f3e7fc7d27--- /dev/null+++ b/core/src/test/java/org/apache/gravitino/storage/relational/utils/TestSessionUtils.java@@ -0,0 +1,90 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one+ * or more contributor license agreements. See the NOTICE file+ * distributed with this work for additional information+ * regarding copyright ownership. The ASF licenses this file+ * to you under the Apache License, Version 2.0 (the+ * "License"); you may not use this file except in compliance+ * with the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing,+ * software distributed under the License is distributed on an+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+ * KIND, either express or implied. See the License for the+ * specific language governing permissions and limitations+ * under the License.+ */++package org.apache.gravitino.storage.relational.utils;++import static org.junit.jupiter.api.Assertions.assertThrows;+import static org.mockito.Mockito.mockStatic;+import static org.mockito.Mockito.never;++import org.apache.gravitino.storage.relational.session.SqlSessions;+import org.junit.jupiter.api.Test;+import org.mockito.MockedStatic;++public class TestSessionUtils {++ @Test+ public void testDoWithCommitShouldRollbackOnAssertionError() {+ Object mapper = new Object();++ try (MockedStatic<SqlSessions> mockedSqlSessions = mockStatic(SqlSessions.class)) {+ mockedSqlSessions.when(() -> SqlSessions.getMapper(Object.class)).thenReturn(mapper);++ assertThrows(+ AssertionError.class,+ () ->+ SessionUtils.doWithCommit(+ Object.class,+ ignored -> {+ throw new AssertionError("boom");+ }));++ mockedSqlSessions.verify(SqlSessions::rollbackAndCloseSqlSession);+ mockedSqlSessions.verify(() -> SqlSessions.commitAndCloseSqlSession(), never());+ }+ }++ @Test+ public void testDoWithCommitAndFetchResultShouldRollbackOnAssertionError() {+ Object mapper = new Object();++ try (MockedStatic<SqlSessions> mockedSqlSessions = mockStatic(SqlSessions.class)) {+ mockedSqlSessions.when(() -> SqlSessions.getMapper(Object.class)).thenReturn(mapper);++ assertThrows(+ AssertionError.class,+ () ->+ SessionUtils.doWithCommitAndFetchResult(+ Object.class,+ ignored -> {+ throw new AssertionError("boom");+ }));++ mockedSqlSessions.verify(SqlSessions::rollbackAndCloseSqlSession);+ mockedSqlSessions.verify(() -> SqlSessions.commitAndCloseSqlSession(), never());+ }+ }++ @Test+ public void testDoMultipleWithCommitShouldRollbackOnAssertionError() {+ try (MockedStatic<SqlSessions> mockedSqlSessions = mockStatic(SqlSessions.class)) {+ assertThrows(+ AssertionError.class,+ () ->+ SessionUtils.doMultipleWithCommit(+ () -> {+ throw new AssertionError("boom");+ }));++ mockedSqlSessions.verify(SqlSessions::getSqlSession);+ mockedSqlSessions.verify(SqlSessions::rollbackAndCloseSqlSession);+ mockedSqlSessions.verify(() -> SqlSessions.commitAndCloseSqlSession(), never());+ }+ }+}