You may have forgotten to add the transactional annotation to AbstractmyprojectService.saveUsers()
Note that internal calls are never proxied, so the thread:
TeacherImportServiceTest.testSaveTeachers() -> AbstractmyprojectService.saveUsers() -> TeacherImportServiceImpl.saveUserObject()
It will not be transactional if AbstractmyprojectService.saveUsers() not transactional.
EDIT
After reading your comments, I see three options for solving it:
- Extract the
saveUsers() method to the saveUsers() class .
Really simple, reorganize your code to support Tx methods in one class and use it from another using composition.
- Use programmatic transactions in
saveUserObject() .
for example, enter 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 of Spring AOP. (I think this option is too complicated just for this problem.)
Using AspectJ, you replace AOP Proxies with Aspect AnnotationTransactionAspect
use <tx:annotation-driven mode="aspectj"/> and select the weaving method (compile or load time).
for weaving boot time, see http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/aop.html#aop-aj-ltw
for compilation time with maven, see Spring / @ Transactional with AspectJ completely ignored
Jose Luis Martin
source share