I need to implement an MVC web service. I selected Spring MVC/Data JPA for this purpose
So my service need to:
- Load some entities
- Make some business logic on it
- Update the entities and store it
- All above need to be in atomic manner
Some code snippet to clarify:
@Service
public class AService {
@Autowired
private Repository1 repository1;
@Autowired
private Repository2 repository2;
@Autowired
private Repository3 repository3;
@Transactional
public Result getResult(Long id) {
Entity1 e1 = repository1.findById(id);
Entity2 e2 = repository2.findById(id);
Entity3 e3 = repository3.findById(id);
e1.setField(doSomeLogic(...)));
e2.setField(doSomeLogic(...)));
e3.setField(doSomeLogic(...)));
repository1.save(e1);
repository2.save(e2);
repository3.save(e3);
return Result.combine(e1,e2,e3);
}
}
I guess ACID is guaranteed here (depends on isolation level?).
How about lock rows which Entities 1-3 represent for the method execution time? Is it possible some other transaction update rows which Entities 1-3 represent while doSomeLogic(...) works? How to improve it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…