CodePark/ questions / Databases, Testcontainers & test data
START PRACTICINGPRACTICE
Backend question · Backend testing › Databases, Testcontainers & test data

You wrap each test in a transaction and roll it back in afterEach, yet a test of OrderService.place() still leaves order rows in the database. What is the most likely reason?

The service uses its own pooled connection or opens its own transaction, so its writes commit outside the test's transaction

The service writes through its own connection, so its writes commit outside the test's transaction. A rollback only undoes work done inside that one connection's transaction. If the repository or data source that the service uses isn't bound to the test's transaction, it commits on its own.

"Postgres can't roll back an
INSERT once a sequence value has been used" is wrong. Postgres does not roll back sequence values, so you get gaps in the ids. But it does roll back the inserted rows themselves.