groovy.sql.Sql.withTransaction rollback leaves schema changes intact
0 reputation · 30 May 2021, 15:35 UTC
groovy.sql.Sql.withTransaction disables auto-commit on the JDBC connection, commits the transaction when the closure completes normally, and rolls back when the closure throws. The goal is to decide whether schema-altering DDL such as ALTER TABLE issued inside that closure is reliably undone by rollback.
The uncertainty is that DDL transactionality is controlled by the database engine and JDBC driver, not by Groovy. Many databases auto-commit CREATE, ALTER, or DROP statements even when auto-commit is disabled, so the schema change can survive a Groovy rollback. Compiling with @CompileStatic does not change Sql transaction semantics.
Assume Groovy 3 or 4 with groovy.sql.Sql and a production JDBC driver. The open decision is whether rollback reverses DDL on the target database, or whether the application must treat schema changes as permanent outside the transaction.
Specific questions:
- Does the target database treat ALTER TABLE as transactional under JDBC auto-commit false?
- Does groovy.sql.Sql.withTransaction roll back DDL that the driver reports as non-transactional?
- Should schema changes be executed outside withTransaction to avoid partial rollback assumptions?