Fix database reopening problems

The database can now be reopened successfully.