2

I was trying to understand an existing code and then I found the use of @Transactional(read-only = true) in the code. I went through this link.(Spring @Transactional read-only propagation). But I cannot relate it to my case.

I am using Hibernate as the ORM tool and uses HibernateTransactionManager to manage the transactions. As per my understanding, code is trying to commit some data to a set of tables and before that I need to fetch some data from two master tables. If the master table query returns nothing, an exception should be thrown. Following is the skeleton of the entire code.

@Transactional
persistSomeData(int id){

    MasterData masterData = getMasterData(id);

    if(null == masterData)throw new Exception("No Master data found !!");

    persistChildTableData(masterData.someField);

}

@Transactional(read-only = true)
MasterData getMasterData(int id){
    //query to get the masterdata using id
}

I want to know the significance of read-only = true in the getMasterData().

Community
  • 1
  • 1
Renjith
  • 1,122
  • 1
  • 19
  • 45
  • `getMasterData` can be called from other places as well. It could be called from services which start read-only transactions. Thumb rule is you create transactions based on what your method wants to do, not based on why / from where it is called. – TheLostMind Oct 21 '16 at 05:50
  • This is the only place where it is used. Did you mean that it has no impact on the parent method transaction? Could you please elaborate? – Renjith Oct 21 '16 at 06:13

0 Answers0