add some logging.
factor out table locking and fix autonumbering bug.