Fixed SQL connections being kept open for longer than needed in many cases, which led indirectly to inability to obtain a database lock.