retain the lock until we have looked up the max seq from the database

the database access must be protected by a lock unless sqlite is built
with full serialization support (but here we can just hold up
releasing the lock until we checked the last seq since it's the next
statment and the operation was intended to be atomic anyway)