facebook

Problem with cached data

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #299453 Reply

    Denis Lavoie
    Participant

    Hi !

    I’m working on a Struts-Hibernate-MySQL project and everything is great expect for a little problem I can’t resolve…

    I’m using Hibernate 3.2, Struts 1.2, MySQL 5.1 and Tomcat 6. I’m also using the DAO classes generated by Hibernate. I’m pretty sure the problem is now related to my setup. My guess is that I’m doing something wrong about the way I’m using the Hibernate Session…

    The context of the project is a Printer Cartridge Recycling Company Website.

    Here my problem :

    I have a table «Cartridge» and there is a list of all cartridge that the compagny recycle… On that page I have a link to a form to add a new cartridge.

    Let say I have two cartridges in the list (and in the Database as well). Then I add a new Cartridge and I’m returned to the cartridge list page. The new cartridge is not present on the list but it is present in the Database. If I refresh the page a couple of time sometime the new cartridge appears sometime it doesn’t…

    If I close my browser and open a new one it does the same… If I shutdown and restart the Tomcat process it work just fine until I add or modify a cartridge…

    My guess is that the Hibernate Cache hold a «out of date» resultset for some hibernate session.

    When I add, modify or delete a record I always use a transaction. I commit it and then I close the session. See below :

    ...
    Cartridge myNewCartridge = new Cartridge();
    myNewCartridge.setProductNo(productNo);
    CartridgeDao cDao = new CartridgeDao();
    Transaction tx = cDao.getSession().beginTransaction();
    cDao.save(myNewCartridge);
    tx.commit();
    cDao.getSession().close();

    To get the Cartridge List I do something like this (In a Struts action) :

     ...
    CartridgeDAO cDao = new CartridgeDAO();
    Criteria crit = cDao.getSession().createCriteria(Cartridge.class);
    crit.addOrder(Order.asc("productNo"));
    crit.setMaxResults(nbr);
    crit.setFirstResult(idx);
    ArrayList<Cartridge> list = (ArrayList<Cartridge>) crit.list();
    if(list.size()>0)
    {
          request.setAttribute("cartridges",list);
    }
    return mapping.findForward("default");

    Then the list is printed on the jsp page.

    I’ve been searching for an example of something similar without any success…

    Any help would be very much appreciated !

    Regards !

    #299523 Reply

    Loyal Water
    Member

    Moving to Off Topic >> Software Development.

    #299527 Reply

    Brian Fernandes
    Moderator

    jedy,

    The code you use to create a new cartridge looks just fine, I couldn’t find anything wrong with it. Perhaps there is a logical error in the code you’re using to retrieve the list or somewhere else – you’re getting the updated list but the view isn’t updating as it should?

    To help debug the issue, I would suggest you try retrieving the entire list to see if the new cartridge made it to that list. Use the findAll method in the DAO to get all the elements.
    Hope this helps.

    #299528 Reply

    Denis Lavoie
    Participant

    Hi Brian,

    Thank you for the quick answer.

    I would like to make a test by bypassing the Hibernate L1 cache… Is it possible to do that?

    If so I could check my theory that the cartridge list sometime comes from the cache even after an update of a record…

    The strange thing is if I refresh the page a couple of time the list shows the update data and sometime it doesn’t… I mean I press CTRL-F5, the list is fine… I press once again CTRL-F5 and it shows the old data.

    It looks like some Hibernate sessions hold the old data for a while in the cache…

    I might be wrong though 😉

    Thanks for any help !

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: Problem with cached data

You must be logged in to post in the forum log in