add try/finally to make sure the db session is always closed, even on error
[Fixes #47990589]