We are using Spring 4.x and Spring Data JPA with declarative transaction management, I have a Controller, Service and a Repository like below pseudo code.
@Service
@Transactional(readOnly=true)
public class SampleService {
@Autowired
private SampleRepository sampleRepository;
@Transactional
public MyEntity saveMyEntity(MyEntity entity) {
//do some business logic
return sampleRepository.save(entity);
}
}
public class SampleController {
@Autowired
private SampleService sampleService;
public String saveSample(@Valid MyEntity entity) {
//Validation
//If Valid
sampleService.saveMyEntity(entity);
//After saving do some view related rendering logic
//Assume here view related rendering logic throws Exception
return "view"
}
}
In the above code an error gets thrown after call to sampleService.saveMyEntity(entity); but the transaction doesn't mark for rollback, so end user will get an error page but behind the scene entity got persisted.
Is there any way i can rollback the transaction ?