What you need is a new transaction just to save what you need to save. You can achieve this by configuring the propagation
of @Transactional
annotation to REQUIRES_NEW
.
Unfortunately your case is a bit tricky, because you are invoking a method within this
context when you do save(items);
, this means the transaction interceptor will not intercept such invocation, therefore you have the possibility to inject the service to a field hold by itself and invoke it on the injected service instead of this forcing the invocation to that method be intercepted by the transaction interceptor, please try the following implementation:
@Service
public class DefaultAnalyzerService implements AnalyzerService {
@Inject
AnalyzerDao analyzerDao;
@Inject
AnalyzerService analyserService;
@Transactional
@Override
public void analyze() {
List<AnalyzerResult> items;
// code filling items list removed;
analyserService.save(items);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public void save(List<SomeType> items) {
analyzerDao.save(items); // <--- Why after call save items are not saved in DB?
}
}
Another thing that changed was the visibility of save(List<SomeType> items)
, that is public now on in order to be intercepted by transaction interceptor and an interface was extracted. This is needed due to limitations with spring, but you can use AspectJ to handle such interceptor, therefore please take a look here.