0

I just saw two different projects.

In one of them whenever you tried to persist/save an entity from a method with no @Transactional annotation there was an exception that said: No session found

On the other

Even though no @Transactional annotation exists the save method allows saving.

PS - One project uses Spring + Hibernate (with session not found exception), the other uses Spring + JPA + Spring Data repositories (allows saving with no transactional annotation)

Any ideas why the differences? What's the best practice?

Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36
Urbanleg
  • 6,252
  • 16
  • 76
  • 139
  • Are you sure that there wasn't an `@Transactional` method somewhere on the call stack of the second project? It's advice that gets wrapped around a method call, so persistence method doesn't have to have the annotation itself if it's only called from other methods that have the advice. – chrylis -cautiouslyoptimistic- Dec 03 '13 at 14:12
  • pretty sure, ill double check – Urbanleg Dec 03 '13 at 14:13
  • 2
    Have you checked if there is a @Transactinal annotation at the class? If a class is annotated transactional, this affects all methods from that class. – Jack Dec 03 '13 at 14:23
  • Or maybe transactions via AOP? – K.C. Dec 03 '13 at 15:27
  • 1
    Maybe in the JPA project the transactions are declared in the spring context. Search for txManager in the context (e.g. JpaTransactionManager) and track it's usage... – Evgeni Dimitrov Dec 03 '13 at 19:01

1 Answers1

1

That's because, as the documentation of Spring Data JPA indicates

CRUD methods on repository instances are transactional by default. For reading operations the transaction configuration readOnly flag is set to true, all others are configured with a plain @Transactional so that default transaction configuration applies. For details see JavaDoc of Repository.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255