do not emit COMMIT/ROLLBACK twice; do not commit/rollback from "closed" state

Fixes #8 and #9. (I tried to create this as two separate pull requests, but the second commit was automatically pulled in.)