Correct finalizing and closing the DB