I created an aspect containing the @Transactional annotation. My advice is invoked as expected, but the new AuditRecord object is never stored in the database, it looks like my @Transactional annotation is not working.
@Aspect @Order(100) public class ServiceAuditTrail { private AppService appService; private FooRecordRepository fooRecordRepository; @AfterReturning("execution(* *.app.services.*.*(..))") public void logAuditTrail(JoinPoint jp){ Object[] signatureArgs = jp.getArgs(); String methodName = jp.getSignature().getName(); List<String> args = new ArrayList<String>(); for(Object arg : signatureArgs){ args.add(arg.toString()); } createRecord(methodName, args); } @Transactional private void createRecord(String methodName, List<String> args){ AuditRecord auditRecord = new AuditRecord(); auditRecord.setDate(new Date()); auditRecord.setAction(methodName); auditRecord.setDetails(StringUtils.join(args, ";")); auditRecord.setUser(appService.getUser()); fooRecordRepository.addAuditRecord(auditRecord); } public void setAppService(AppService appService) { this.appService = appService; } public void setFooRecordRepository(FooRecordRepository fooRecordRepository) { this.fooRecordRepository= fooRecordRepository; } }
The bean context is as follows:
<tx:annotation-driven transaction-manager="txManager.main" order="200"/> <aop:aspectj-autoproxy /> <bean id="app.aspect.auditTrail" class="kernel.audit.ServiceAuditTrail"> <property name="appService" ref="app.service.generic" /> <property name="fooRecordRepository" ref="domain.repository.auditRecord" /> </bean>
My pointcut only intercepts interfaces (service interfaces). Service methods may or may not be transactional. If the service method is transactional, I would like this transaction to be discarded if for some reason an error occurred due to an error.
My question is: why is the transactional annotation ignored? This is my first time we created the AOP service with Spring, I also welcome any improvements in architecture or implementation.
Thanks!
java spring-aop hibernate transactions
levacjeep
source share