Spring Framework
@EnableTransactionManagement
Enable annotation-driven transaction management with an appropriate transaction manager. Spring 7.0.9 instructional lab; source-reviewed, not executed.
Formal Syntax & Structure
java syntaxjava
@EnableTransactionManagementWorking Example
Example Snippetjava
package lab.spring;import org.springframework.context.annotation.*;import org.springframework.transaction.*;import org.springframework.transaction.annotation.*;import org.springframework.transaction.support.SimpleTransactionStatus;public class TransactionDemo { // Recording test double only: no resource, isolation or durability. public static class RecordingManager implements PlatformTransactionManager { public TransactionStatus getTransaction(TransactionDefinition definition) { System.out.println("begin"); return new SimpleTransactionStatus(); } public void commit(TransactionStatus status) { System.out.println("commit"); } public void rollback(TransactionStatus status) { System.out.println("rollback"); } } public static class Service { @Transactional public void work(boolean fail) { System.out.println("work"); if (fail) throw new IllegalStateException("test failure"); } } @Configuration(proxyBeanMethods=false) @EnableTransactionManagement static class Config { @Bean PlatformTransactionManager transactionManager() { return new RecordingManager(); } @Bean Service service() { return new Service(); } } public static void main(String[] args) { try (var context = new AnnotationConfigApplicationContext(Config.class)) { var service = context.getBean(Service.class); service.work(false); try { service.work(true); } catch (IllegalStateException expected) { if (!"test failure".equals(expected.getMessage())) throw expected; } } }}