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 .
source share