fix connection leak for iterator in on-demand sqlobject

I noticed that on-demand sqlobject was leaking connections when a SQL statement fails to execute. This can happen for example when there is a syntax error, a bad parameter, or an unavailable resource on the database.

While fixing, I noticed two other potential problems. I fixed them on two separate commits.