
670
Chapter 25 Database Patterns
Transaction Rollback Pain
John Hurst sent me an e-mail in which he described some of the issues his
team had encountered using Transaction Rollback Teardown. He writes:
We used Transaction Rollback Teardown for our database integra-
tion tests for a while, after a discussion on TheServerSide during
which Rod Johnson advocated the approach. I gathered his main
motivation for using it was for performance; a rollback is usually
a lot faster than repriming the database in a new transaction for
the next test. Indeed, we did fi nd it somewhat faster than our pre-
vious approach. We used Spring’s excellent
AbstractTransactionalData-
SourceSpringContextTests
base class, which supports most of what you
need to do for this pattern out of the box.
However, I chose to abandon this pattern after a few months.
Here are the drawbacks I came across with this approach:
1. You lose some test isolation. In the way we implemented this
pattern, anyway, each test assumed the database was in a cer-
tain base starting condition, and the rollback would revert it
to that condition. In our current model, each test is respon-
sible—usually via a base class’s setUp()—for priming the data-
base into a known state.
2. You can’t see what’s in the database when something goes
wrong. If your test fails, you usually want to examine the
database to see what happened. If you’ve rolled back all the
changes, it makes it harder to fi nd the bug.
3. You have to be very careful not to inadvertently commit
during your test. Yes, the code under test has declarative
transaction management, and does nothing surprising. But
we occasionally would need to do things in the test setup
like drop and recreate a sequence to reset its value. This,
being DDL, commits any outstanding transaction—and
confused programmers.
4. You can’t easily mix in tests that do need to commit changes.
Lately I have added some PLSQL stored procedures and tests.
Some of the stored procedures do explicit commits. I cannot
mix these in the same JUnit suite with tests that assume the
database always remains in a certain state.
I apologize if my terminology isn’t consistent with what’s in your
book. Also, my experience is probably a little limited; I’ve only
Transaction
Rollback
Teardown