Database architecture

@Transactional rollbackFor

Configure rollback for a checked exception in a managed invocation. Source-reviewed PostgreSQL/Spring lab or manual procedure, NOT RUN.

Formal Syntax & Structure

java syntaxjava
@Transactional(rollbackFor = IOException.class)

Working Example

Example Snippetjava
// Illustrative Spring service, not standalone; NOT RUN.package example.tx;import java.io.IOException;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Servicepublic class CheckedWriter {    private final JdbcTemplate jdbc;    public CheckedWriter(JdbcTemplate jdbc) { this.jdbc = jdbc; }    @Transactional(rollbackFor = IOException.class)    public void reserveThenFail(String sku) throws IOException {        int changed = jdbc.update("UPDATE inventory SET stock=stock-1 WHERE sku=? AND stock>0", sku);        if (changed != 1) throw new IllegalStateException("not available");        throw new IOException("synthetic failure");    }}
Java Reference