@Transactional
vs @Transactional(readOnly=true)
:
The boolean readOnly
is by default, which means @Transactional
= @Transactional(readOnly=false)
, which effectively means you are hinting that the method would require write access. For a method that doesn't need data modification, you would annotate it with readOnly=true
. This doesn't mean that using the annotation will automatically handle that for you, though - you will still need your entity managers, etc set up to effectively use it. Check out this page for more info.
One main reasons why we put the annotation on the Service layer and not the DAO layer, is we usually define the business logic on the service layer, and a logic may require multiple database interactions, and having it on the service layer encapsulates all the db calls will be done in one transaction (e.g. a rollback on all the db calls if it fails, etc).