Fix db handle leaks for Query