core: fix problem where z_db:insert can hang when running out of db connections.

(cherry picked from commit ba80328740183bd56bcfcd4f30414efa35df608b)