ziqiangliang commented on issue #7378: URL: https://github.com/apache/gravitino/issues/7378#issuecomment-2975017394
> [@ziqiangliang](https://github.com/ziqiangliang) Hi, thanks for your work, I'm very confused about the format. > > public static <T, R> Operation<T, R> callWithoutCommit( > Class<T> mapperClass, Function<T, R> function) { > return new Operation<>(mapperClass, function); > } > Why is it able to solve the connection leakage problem? When will the connection be closed? Did I miss something? Hi, great question! You're right — just looking at `callWithoutCommit(...)` itself, it seems like nothing changes regarding connection management. But the key is: **this method doesn't actually open a connection** or do any DB operation **immediately**. Instead, it just **wraps the logic into an `Operation` object**, which gets passed later into: ```java SessionUtils.doMultipleWithCommit(Operation... ops) ``` This `doMultipleWithCommit(...)` method is where: * The DB connection is opened (inside a try-with-resources block), * All the `Operation#execute()` calls are run using the same connection, * And finally, the connection is committed or rolled back and closed properly. Because the `execute()` method of `Operation` is **package-private**, you can't accidentally run the operation outside of this method. So, compared to the old `doWithoutCommit(...)` style (which would **immediately** get a connection and potentially leave it open if misused), this new design **prevents misuse** and **avoids connection leaks**. Hope that helps! Let me know if you'd like me to walk through a code example. --- > So essentially, this API design guides the developer toward the safe usage pattern and makes the wrong usage harder or impossible. -- 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]
