Home > AI > Backend > SpringBoot > mysql-connector-java >

@Transactional

This annotation means Spring AOP will wrap the code like this

@Transactional
public void businessLogic() {
 ***

}

is same as

Transaction t = entityManager.getTransaction(); 

try { 
    t.begin();

    businessLogic();

    t.commit(); 
} catch(Exception ex) { 
    t.rollback(); 
    throw ex; 
} 

Namely, if any errors happen, the transaction will not be saved into the database.

Secondly, if you add @Transactional in Test, it will by default roll back.

spring documentation

By default, the framework will create and roll back a transaction for each test.[..]

Leave a Reply