Everything works fine on local, but when deployed I get a ConcurrentModificationException thrown when I use session.setAttribute() from a servlet. I use this to return a value from the datastore after using an HTML form to add an Entity.
How come and what could I do about it?
Here is the broken servlet:
public class AdminServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Boolean logged = false;
HttpSession session = req.getSession(true);
Enumeration<String> names = session.getAttributeNames();
while(names.hasMoreElements()) {
String name = (String) names.nextElement();
if(name.equals("logged")) {
logged = (Boolean) session.getAttribute("logged");
}
}
if(logged) {
String p1 = req.getParameter("name");
String p2 = req.getParameter("value");
if(p1 != "" && p2 != "") {
Entity e= new Entity("MyEntity");
e.setProperty("name", p1);
e.setProperty("value", p2);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction();
Key k = datastore.put(e);
txn.commit();
if(txn.isActive())
txn.rollback();
session.setAttribute("returnedKey", KeyFactory.keyToString(k)); // CRASHES HERE
}
}
resp.sendRedirect("adminpage.jsp");
}
}
Thanks!
You have an Enumeration open on the session as well.
Changing attributes in session would affect name and that is why you are seeing the error.
Try writing your code so that names is not in use (nulled or out of scope):
It’d be easy to put it in a method