Maybe you forgot to add the transactional annotation to AbstractmyprojectService.saveUsers()
Note that inner calls are never proxied, so the flow:
TeacherImportServiceTest.testSaveTeachers() ->
AbstractmyprojectService.saveUsers() ->
TeacherImportServiceImpl.saveUserObject()
Will not be transactional if AbstractmyprojectService.saveUsers()
is not transactional at all.
EDIT
After reading your comments, I see three options to solve it:
- Extract the
saveUsers()
method to a helper class.
Really simple, refactor your code to keep the Tx methods in one class and use it from another one via composition.
- Use programmatic transactions in
saveUserObject()
.
for example, inject the txManager and
public void saveUserObject() {
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
TransactionStatus status = txManager.getTransaction(def);
try {
// the code
}
catch (Exception ex) {
txManager.rollback(status);
throw ex;
}
txManager.commit(status);
}
- Use AspectJ instead Spring AOP. (I think that this option is too complex only for this issue.)
Using AspectJ you replace AOP Proxies wiht the Aspect AnnotationTransactionAspect
use <tx:annotation-driven mode="aspectj"/>
and choose a weaving method (compile or load time).
for load time weaving, see http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/aop.html#aop-aj-ltw
for compile time weaving with maven, see Spring / @Transactional with AspectJ is totally ignored