Skip DB flush for read-only mode.

Only flush databases on close when they were in read-write mode.  Plus some minor clean-up of the name database code.