It was interesting to read the article from Vlad Mihalcea about "Read-write and read-only transaction routing with Spring".
But still i could not direct understand what will happen in this kind of situation:
@Transactional
public Service(Repository z, Repository y) {
public void doSomething(){
z.findAllByXYZ() -> readOnly=true on repository level
z.save();
y.save();
}
}
Do we have here a problem or some disadvantages? will findAllByXYZ be fetched from the replica and the save will be processed against the primary node?
What if we dont have any replica at all, is it safe to do it like in this snippet?
For your first case - this depends on the propagation
property of the Transactional
annotation. If you haven't specified this, the read-write data source will be used and all data is read from and written to the same database instance which is a good thing for data consistency. If you were to use Propagation.REQUIRES_NEW
then this would actually be using a new transaction which then would use the read only attribute and hence read from the replica.
If you don't have a replica how did you specify the datasource for the replica in the first place if you are following the example of the mentioned blog post? If it's the same as for the primary data source than this is just another jdbc connection where you might get data consistency issues since you might not read data that was previously written in the suspended transaction when using propagation REQUIRES_NEW
.