9

I'm using a read only database to get some data in to my project. We use Spring v3 with jpa and hibernate

Will the following annotation have the effect of making all calls to my repository a read only transaction? Or do I need the annotation on the service layer calling the repository

package com.blah.jpa;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly = true)
public interface ServiceToServerRepository extends JpaRepository<ServiceToServerJPA, String> {

}

So we get all the findOne, findAll for free. But any updates should fail as it is a read only transaction

t0mmyw
  • 737
  • 3
  • 10
  • 22

2 Answers2

9

That should basically do it. But I usually opt for marking service-layer artifacts with Transactional annotation. Anyway, please also check this post for more information of using transactionality together with Spring Data: How to use @Transactional with Spring Data?

Community
  • 1
  • 1
Wojciech Owczarczyk
  • 5,595
  • 2
  • 33
  • 55
6

The readOnly flag only hints that it is sufficient with a read only transaction while calling the method.

If you have a nested transaction from your service layer which is not read only, then it would use that one, and you will effectively disregard this annotation.

Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44