Fix database finalization
Using try: ... finally: ... to fix random segmentation faults on exceptions