Spring @Transactional does not start a transaction

I have a multi-module Maven project with the following structure:

project | -- data | -- DepartmentRepository.java | -- domain | -- Department.java | -- service | -- DepartmentService.java | -- DepartmentServiceImpl.java | -- web | -- DepartmentController.java 

The project uses Spring 4.1.4, Spring data JPA 1.7.2 and Maven 3.1.0. The following classes are included:

 @Entity class Department {} interface DepartmentRepository extends JpaRepository<Department, Long> {} interface DepartmentService { List<Department> getAll(); } @Service @Transactional(readOnly = true) class DepartmentServiceImpl implements DepartmentService { @Autowired private DepartmentRepository departmentRepository; @Transactional public List<Department> getAll() { return departmentRepository.findAll(); } } 

I was hoping that as soon as the code went into DepartmentServiceImpl.getAll , the transaction would start. However, I find that it is not. No transaction is running. I checked this by looking at TransactionSynchronizationManager.isActualTransactionActive() inside this method, which prints false . I also checked by setting breakpoints in TransactionAspectSupport.invokeWithinTransaction . However, as soon as departmentRepository.findAll is called, the transaction is started correctly (since SimpleJpaRepository , the class that provides the implementation of the JPA repository interface, is also annotated using @Transactional ).

A complete sample application demonstrating the problem is available on Github .

0
source share
1 answer

I noticed that your annotation-driven mode is set to aspectj

 <transaction:annotation-driven mode="aspectj"/> 

but you do not have load-time-weaver defined in your context.

This may or may not be a problem, as I just looked quickly. Also, I don’t understand why you will need aspectj vs default proxy with what you have, so you can just remove mode="aspectj" in general and the default proxy.

0
source

Source: https://habr.com/ru/post/927961/


All Articles