2

Im currently building a shopping cart and I want to update the item stock in my database for each item in my cart after checkout.

The items are stored in Session Scope as list of Java Beans. This is my checkout servlet.

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {

    final HttpSession session = request.getSession();
    @SuppressWarnings("unchecked")
    List<CartItems> itemsInWc = (List<CartItems>) session.getAttribute("Cart");

    for (CartItems item : itemsInWc) {
        updateBestand(item.getInCart(), item.getSize(), item.getId(), item.getKat(), item.getName(), item.getBestand());
    }
}

First I´m getting the List with items then I iterate through eatch item and update the stock with the method "updateBestand".

public void updateBestand(int inCart, String size, int id, String kategorie, String name, int bestand)
                throws ServletException, IOException {
    
    int updateAnzal = bestand - inCart;

    try (Connection con = ds.getConnection();
                    PreparedStatement pstmt = con.prepareStatement("UPDATE " + kategorie + " SET " + size + " =?"
                                    + " WHERE artikelName = ? and id = ?")) {

        pstmt.setInt(1, updateAnzal);
        pstmt.setString(2, name);
        pstmt.setInt(3, id);

        pstmt.executeUpdate();

    } catch (Exception ex) {
        throw new ServletException(ex.getMessage());

    }

}

But Everytime I call my checkOut-Servlet, it just update the stock of the first item in the list and then it thorws a ConcurrentModificationException.

java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at Servlets.UpdateBestandServlet.doGet(UpdateBestandServlet.java:40)
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
DD-DD
  • 21
  • 1

0 Answers0