Testing transactional aspect of your application is not easy as we usually use Springs’ transaction rollback on tear down testing approach. Though there are solutions to test aspect oriented logic it’s not without a price. More than that – we very much got used relying on easy-to-use Spring @Transaction annotation so that we don’t usually take an effort to do it. There is a few standard Spring rules for rollbacking transaction in relation to method resolution:
- transaction is automatically rollbacked only on unchecked exeption (RuntimeException)
- rollback will also occur for exception types specified in rollback-for attribute of the annotation or XML element
- rollback will not occur for exception types specified in no-rollback-for attribute of the annotation or XML element
- transaction demarcation will aplly only to the external calls of the bean method (consider use-case when external logic calls a public method on your bean, that is not annotated with @Transactionaidl and this method will call in its body another public method of the same class that has @Transaction annotation – in such case Spring will not start and manage transaction)
But there is yet another one – your @Transactional annotation must be resolved by Spring bean post processing logic in the first place. Usually it is, but when using CgLib based proxies (proxy-target-class) there is a catch which will cause omitting @Transactional annotation on your method declaration.