Is Spring capable of using different database endpoints for failover and query routing - ie, reads go to the read cluster, writes go to the master? And specifying which endpoint to use per Method in a Service
class
For example, say we have the following Service. The save
and delete
can go to the master endpoint to write, whilst the rest can go to the read cluster
.
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
@Autowired
private AttributeService attributeService;
// -------------------------------------------------------------
private boolean nameAvailable(String name, Long id) {
if (id == null) {
return productRepository.findByNameEquals(name) == null;
} else {
return productRepository.findByNameEqualsAndIdIsNot(name, id) == null;
}
}
public synchronized void save(Product product, BindingResult result) {
// synchronized to avoid name duplication
if (!result.hasErrors()) {
if (!nameAvailable(product.getName(), product.getId())) {
result.addError(new FieldError("product", "name", "must be unique"));
} else {
productRepository.save(product);
}
}
}
public void delete(Long id) {
productRepository.delete(id);
}
// -------------------------------------------------------------
public Page<Product> getProducts(PageRequest pageRequest) {
return productRepository.findAll(pageRequest);
}
public Product getProduct(Long id) throws ProductNotFoundException {
Product product = productRepository.findOne(id);
if (product == null) {
throw new ProductNotFoundException();
}
return product;
}
public Page<Attribute> getAttributes(Product product, Pageable pageable) {
return attributeService.getAttributes(product, pageable);
}
}