Failed to commit transaction when rolling back schema changes in Cucumber @After hook
0 reputation · 12 Feb 2025, 02:03 UTC
Rollback safety after a schema change in Cucumber
When a scenario creates or alters tables in an @Before hook, the test framework often relies on the underlying ORM to begin a transaction and then rolls it back in an @After hook. However, many databases do not support rolling back DDL statements, leading to the documented error Failed to commit transaction when a hook attempts to commit a transaction that was never opened or has already been rolled back.
The main constraints are:
- DDL changes are not transactional in most relational DBs.
- Nested transactions or savepoints may be ignored by the driver.
- Shared test databases risk residual schema artifacts across scenarios.
Given these uncertainties, the following questions remain:
- What is the safest way to revert DDL changes made in a
@Beforehook without triggering aFailed to commit transactionerror? - Does Cucumber provide any built‑in support or recommended pattern for per‑scenario isolated databases?
- How can we detect and handle a failed transaction commit in an
@Afterhook to ensure clean test isolation?