I have some code that does database write operations, and I want to implement a dry-run, which means I want to execute the same code, but rollback the transaction at the end, even if no error occurred.
I have wrapped my code in a method annotated with @Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW) but the behavior is not what I expect. Instead of rolling back the transaction it opens a read-only transaction in the database and attempts to write, then I get the following error:
ERROR: cannot execute INSERT in a read-only transaction
I know the existence of the @Rollback annotation but it seems to be only for unit testing purposes.
How do I run code that writes in the database with a final rollback?