2
I have a web application selling school canteen product, this process is accessed by several users simultaneously.. I have a method that updates and validates if quantity is available.. I need this method to be executed by one user at a time. I tried to put Synchronized but did not succeed, follow the excerpt:
public synchronized void atualizarQuantidadeItemCardapio(Cardapio itemCardapio) throws NegocioException {
for (MovimentoCantina itemVenda : movimento) {
if (itemCardapio.getProduto().getCodigo().equals(itemVenda.getProduto().getCodigo())) {
validarProdutoEsgotado(itemCardapio);
if (!estaVazio(itemCardapio.getQuantidade())) {
itemCardapio = atualizarItemCardapio(itemCardapio);
itemCardapio.setTransacao(persistencia);
diminuirQuantidadeCardapio(itemCardapio, itemVenda);
itemCardapio.preencherSituacao();
itemCardapio.alterarQuantidade();
itemCardapio.alterarVersao();
criarLogCardapio(itemCardapio, itemVenda, CardapioLog.VENDA);
}
}
}
}
There was some execution/compilation error ?
– Isaías de Lima Coelho
If I understand correctly, you just want to update the amount of items, if that is it, there is no need to be Synchronized. What you need is to know if it is available, right? You will have to perform the consultation before the purchase is completed.
– Filipe L. Constante
Yes.. I am doing the query but as two users are doing the same query at the same.. they take the same amount and make the purchase. Example: I have a certain product whose quantity is 1.. both buy this product at the same time.. so when they perform the query they both check that it is 1 so it has available.. both finalize the sale but it was for one of them to receive the unavailable product error.
– Pedro Henrique