I'm running as unit test and it automatically goes back even if I don't use the@rollback
in spring 3.1.
My test looks like
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" })
public class PersonServiceTest {
@Test
@Transactional
public void savePerson() {
Person person = createPerson();
personService.savePerson(person);
}
}
Is Rollback behavior is set by default?
The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.
So when you annotate a method with @Transactional , Spring dynamically creates a proxy that implements the same interface(s) as the class you're annotating. And when clients make calls into your object, the calls are intercepted and the behaviors injected via the proxy mechanism.
Note however that the Spring Framework's transaction infrastructure code will, by default, only mark a transaction for rollback in the case of runtime, unchecked exceptions; that is, when the thrown exception is an instance or subclass of RuntimeException. (Errors will also - by default - result in a rollback.)
By default, the only exceptions that cause a transaction to roll back are the unchecked exceptions (like RuntimeException ).
By default SpringJUnit4ClassRunner
will rollback transactions automatically.
To negate the effect, use @TransactionConfiguration(defaultRollback=false)
on your test class or @Rollback(false)
on each test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With