Using transactions to prevent committing when errors occur