Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix releasing of resources in case connection initialization failed #11915

Merged
Merged
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 @@ -38,6 +38,11 @@ private[backend] case class InitHookDataSourceProxy(
} catch {
case t: Throwable =>
logger.warn(s"Init hook execution failed", t)
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also use Using:

Using(connectionBody) { connection => 
  initHook(connection)
}.fold(t => throw t, identity) 
  • Connection is AutoCloseable, so should be closed by Using
  • Using also handles exceptions in connectionBody (unlike the current code)

Not sure what happens if just the closing of the connection fails though, we probably do not care about that error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a lot more easier to read in the form @rautenrieth-da proposed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't Using actually close the connection anyway?

connection.close() // releasing resources in case of initialisation issues
} catch {
case _: Throwable => () // catching all resource-releasing exceptions
}
throw t
}
logger.info(s"Init hook execution finished successfully, connection ready")
Expand Down