rollback for a list

If I make looping on a list and inside the loop I check for a boolean in the looping object if true change and commit it and if false break the loop and rollback the whole list (even the just commited objects) what is the best way to do this?
1 answers

Keep the status of the result of the loop in a variable (Create the variable before the loop) and rollback outside the loop based on the loop result. Don't commit inside the loop. Only change. If all ok, run a new loop and commit all.

Or use something with batch from community commons.