Search code examples
springspring-boottransactionsspring-data

For two methods read and write in the same Transactional service, read data before it is committed to DB by the write methods transaction


My service has two methods persistLatitude and findLatitude

@Service
@Transactional
public myservice1{
 void persistLatitude (Long Latitude){
      //some code
}   
Long findLatitude (){
  //some code
}

Let's say I persist the latitude 29° 52' N in the my DB table but the method persistLatitude continues to run for some other tasks. As per my understanding, because of the transactional annotation my changes will only be committed to DB when this entire method has finished execution.

While this is going on, the rest endpoint receives a request to find the latitude 29° 52' N from the DB table. As the transaction hasn't been committed on the account of method not being complete, the findLatitude won't be able to find 29° 52' N in the table.

Is there anything I can do to enable findLatitude to retrieve this latitude when the transaction that would create this value is not fully complete?


Solution

  • The feature you are asking for is called "isolation level read uncommitted". You can apply it for a class or method:

    @Transactional(isolation = Isolation.READ_UNCOMMITTED)
    Long findLatitude (){
        //some code
    }
    

    This feature is not supported by Postgres and Oracle. Mainly because of side effects like phantom or non-repeatable reads.