0

Question is a follow on from CrudRepository and Hibernate: save(List<S>) vs save(Entity) in transaction.

If I do not mark the following below with @Transactional. If the database connection drops or something happens as Spring Data JPA is saving a list. Will the list be rolled back or it will just be partly saved?

@Transactional
public void processData() {
   List<MyEntity> entities = ....;
   MyEntityRepository.save(entities);
}
Andronicus
  • 25,419
  • 17
  • 47
  • 88
Blawless
  • 1,229
  • 2
  • 16
  • 26

2 Answers2

2

Methods provided by CrudRepository are already transactional.

This is not needed at all, just take a look at this implementation. All the methods delete, deleteAll,saveAllare annotated with@Transactional`. This means, that default implementation already takes it into account.

Actually whereever there is no @Transactional annotation, the default @Transactional(readOnly = true), that is on the class level is used.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
1

It depends heavily on the MyEntityRepository implementation.

E.g. If it is generated by spring as a SimpleJpaRepository then it will be rollbacked. Because as shown in the linked question the save() method is also annotated with @Transactional:

@Transactional
public <S extends T> List<S> save(Iterable<S> entities) {}

But for every other implementation? I would say that behaviour is undefined

Lino
  • 19,604
  • 6
  • 47
  • 65