Junior — Middle
How is the final result of a compiler's work represented after the source code compilation process?
sobes.tech AI
Answer from AI
In a microservices architecture, sharing a single database among multiple services is generally avoided to prevent tight coupling. However, if a shared database is used, updates for each microservice are performed using transactions that ensure data integrity.
Common approaches include:
- Database-level transactions — each microservice performs its operations within a transaction to make changes atomic.
- Optimistic locking — to prevent conflicts during concurrent updates.
- Event Sourcing and CQRS — updates happen through events that are applied to the database, allowing synchronization of state.
Example in Java with Spring and JPA:
@Transactional
public void updateEntity(EntityDto dto) {
Entity entity = repository.findById(dto.getId())
.orElseThrow(() -> new EntityNotFoundException());
entity.setField(dto.getField());
repository.save(entity);
}
Here, the @Transactional annotation guarantees that the update is performed atomically.