1

I'm trying to code a jsp webpage showing users in the database. The page is working corretly.

For example it's possible to remove users from database by clicking a link on the page, but unfortunately the user is still shown on the next load of the page. After reloading the page the user is gone. I added a @Cacheable(false) to the JPA class but nothing changed.

For a better understanding an example workflow:

  1. Admin clicks on Usermanagement (calling "showUserHandler" servlet)
  2. Page is loading with the first 10 users from database
  3. Admin deletes User ( by calling a handler(MVC)/servlet)
  4. Servlet deletes the user and calls the showUserHandler from point number 1
  5. Page is loading, showing 10 users, but the deleted user is still there
  6. Pressing F5 or "reload" and the user is gone

The user is removed like:

Users user = em.find(Users.class, request.getParameter("uId"));
if (user != null) {
em.getTransaction().begin();
em.remove(user);
em.getTransaction().commit();
em.close();
}

And loading the user:

EntityManager em = SessionUtil.createManager();
List<Users> usersList = em.createNamedQuery("Users.findAllOrdered", Users.class).setParameter("uId", Integer.parseInt(request.getParameter("lastID"))).setFirstResult(0).setMaxResults(10).getResultList();

Can someone tell me whats wrong ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Keine-Ahnung
  • 601
  • 1
  • 5
  • 15
  • Can you share the complete source code of Step 3 servlet where deletion and calling of showUserHandler takes place? – Sanjeev Saha Jun 15 '16 at 17:03
  • 1
    why not look in the log of the JPA provider? then you see the SQL invoked and KNOW whether it is in the datastore or not, and can see if it is coming from the cache or not. Just relying on JPA API calls is treating it as a big black box and not debugging much at all – Neil Stockton Jun 15 '16 at 18:04
  • @SanjeevSaha sourcecode added – Keine-Ahnung Jun 16 '16 at 06:19
  • @marian04 Could you please tell me the full url shown on the browser when user details are loaded from database? – Sanjeev Saha Jun 16 '16 at 06:48
  • @SanjeevSaha added – Keine-Ahnung Jun 16 '16 at 07:02
  • "Pressing F5 or "reload" and the user is gone" - smells like a browser cache thing. http://stackoverflow.com/questions/5139785/how-to-prevent-the-result-of-servlets-from-being-cached – Gimby Jun 16 '16 at 07:09
  • @Gimby Hey, i tried that already but nothing changed, so i removed that from source again. – Keine-Ahnung Jun 16 '16 at 07:16

1 Answers1

0

I think your code is throwing a java.lang.IllegalArgumentException: Removing a detached instance when you remove the entity, making a rollback in your transaction. This happens because you are loading and removing this entity in a different persistence context.

Did you tried calling the find inside your transaction?

em.getTransaction().begin();
Users user = em.find(Users.class, request.getParameter("uId"));
if (user != null) {
em.remove(user);
em.getTransaction().commit();
em.close();
}
hashiCode
  • 489
  • 8
  • 17