Search code examples
transactionsspring-datarollback

Use transaction for multiple save


I need to save more entities in my service in one transaction - if one save fails, nothing should be saved. I tried to use @Transactional, but it doesn't work, or I don't know, how to use it :) Correct record is saved and bad record is not saved, I need both not to be saved. How can I achieve that?

My code:

public interface MyRecordRepository extends JpaRepository<Record, Long>,
        QueryDslPredicateExecutor<Record> {

}

@Service
public class MyPersisterServiceImpl implements MyPersisterService {...
    @Autowired
    private MyRepository myRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRecords(List<Record> recordList) {
        for(Record r : recordList){
            myRepository.save(record);
        }
    }
...


@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
... // other annotations
public class Application extends SpringBootServletInitializer {
...

My env: Spring 4.0.7 with Spring data, querydsl, hibernate, apache tomcat 8, java 8.

Thank you


Solution

  • It looks like there is a problem with aspectj configuration, because in proxy mode it works as expected... I mark this question as resolved for now.