4

I tried looking several SO questions and spring documents but still could not understand the significance of @Transactional(read-only = true).

can it be only used for read only transactions or it can be use for something like below which actually read and write databse

@Transactional(readOnly = true, propagation = Propagation.REQUIRED
                          , rollbackFor= {Exception.class})
public void doMultipleOperation(MyObj obj) throws Exception{
//call delete DAO method
//call insert DAO method
//call select DAO method
}

I find similar question like this and multiple others on SO but I am looking for answer in more layman term.

want2learn
  • 2,471
  • 2
  • 20
  • 37

1 Answers1

-1

If you set a Transaction to readonly = true, then no, you will not be able to call any service methods that create, update or delete data from the database. This is because the flushMode on the Hibernate session will be set to NEVER, and so you will be prevented from making any changes to your data. Only selects will be allowed.

starman1979
  • 974
  • 4
  • 12
  • 33
  • By defaut it is false, so just put `@Transactional` – starman1979 Mar 22 '18 at 19:06
  • Just to be clear. i will be using readonly = true only with propagation = Propagation.NOT_SUPPORTED –  Mar 22 '18 at 19:08
  • Depends on your context, but you could also set propagation to `REQUIRES_NEW` which will create a new transaction in only a read-only context. But again, I don't know how you are using it so you would have to explore all the options. Take a look at: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Propagation.html – starman1979 Mar 22 '18 at 19:15
  • I am sorry but i could not understand. If you look at my service method in my question, do you think I am doing it correctly ? –  Mar 22 '18 at 20:45
  • As explained by Spring-Data author: https://stackoverflow.com/questions/10394857/how-to-use-transactional-with-spring-data/10466591#10466591 This is not necessarily true, transactional readony doesn't always prevent you to make modifications. That depends on the used database. It's mostly a marker that can be used for optimizations with certain ORMs. – Aníbal Apr 19 '22 at 18:37