facebook

Struts/Hibernate webpage updates are wrong

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #280561 Reply

    awamser
    Member

    I have two tables in an Oracle database. One is Team and the other is Member /w foreign Keys. I used the MyEclipse wizard to build the hibernate mappings / Java Objects and the DAO’s.

    I have a team search page to do a search based on name and allow the user to view the team members. You can click a link to edit one of the users from the results. Seems simple…

    The problem is that after you update the member the updated value will toggle between the old and the new for the member. The new value is being written to the database. But the results on the webpage are not 🙁

    When the edit form is submitted there is a hidden field (member id) on the struts from. In a struts action I use that id to look up the member from the database. Take the values from the form to set the member object.

    Here is the flow…

    1. Struts Search Form -> SearchAction.java

    List teamList = teamDAO.findByTeamName(teamName);

    Request.setAttrbute(“teamList”,teamList);

    2. List Team in jsp
    3. Click Edit href Link (look up from database) -> EditMemberAction.java

    String memberId = request.getParameter(“memberId”);

    Member member = memberDAO.findById(memberId);

    Reqeuest.setAttrbute(“member”,member);

    4. Display the edit member struts from and submit changes -> UpdateMemberAction.java

    // get the team and member from the database us DAO and create hibernate transaction

    memberDAO.attachDirty(member);
    memberDAO.getSession().update(team);
    tx.commit();
    memberDAO.getSession().flush();

    Forward back to view member page…

    Why do I randomly see the old values in the webpage. I hope this all makes sense!

    Thanks!

    #280601 Reply

    Riyad Kalla
    Member

    ok this is a pretty Hibernate-specific question, so I’m going to dig into my brain, but no guarantees…

    tx.commit();
    memberDAO.getSession().flush();

    First, I don’t think session flush is necessary after the trans commit. But that’s just a minor point.

    Second, you *can* see the behavior you are seeing if you have the Level 2 cache in Hibernate enabled. I dug into it quite a bit a few years ago and forget the logistics of it, but this sounds exactly like the behavior you see when it’s enabled. Do you have it enabled by chance?

    #280613 Reply

    awamser
    Member

    No, I don’t have any level 2 cache enabled 🙁 Any other ideas?

    #280614 Reply

    Riyad Kalla
    Member

    hmm… just for kicks, can you try flushing the session before loading the list of members to display on that list page? There is some reason the session is holding stale copies of those entities and passing them back when the list is reloaded… I’m just not sure what the logic in Hibernate is that’s causing it.

    #280615 Reply

    awamser
    Member

    That seems to have fixed the problem… not sure why 🙁

    I do have the following WARN:

    2797 [http-8888-Processor25] WARN net.sf.ehcache.config.Configurator – No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/apache-tomcat-5.5.25/webapps/PP/WEB-INF/lib/ehcache-1.1.jar!/ehcache-failsafe.xml

    But I thought that this was used for L2 caching and the defaults would be ok. Am I missing something?

    #280616 Reply

    Riyad Kalla
    Member

    That seems to have fixed the problem… not sure why 🙁

    Awesome!

    2797 [http-8888-Processor25] WARN net.sf.ehcache.config.Configurator – No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/apache-tomcat-5.5.25/webapps/PP/WEB-INF/lib/ehcache-1.1.jar!/ehcache-failsafe.xml

    But I thought that this was used for L2 caching and the defaults would be ok. Am I missing something?

    AFAIK that is harmless, sort of like the Log4J warning you get when a Struts app spins up about no appenders. If you don’t provide additional configuration to ehcache it just uses the default, but as you stated, if it’s not turned on for L2 then why is it initializing… I’m not sure honestly, I’ve just gotten used to ignoring the ehcache message unless I’m using L2 and *trying* to customize it.

    #280631 Reply

    awamser
    Member

    @support-rkalla wrote:

    That seems to have fixed the problem… not sure why 🙁

    Awesome!

    Ok, I lied! Everything seems ok until i deploy it to production. I use Tomcat for development and Oracle AS for production. The problem is still there… This’ll be fun trying to figure out 🙁

    #280637 Reply

    awamser
    Member

    Mapping file for Team…

    <hibernate-mapping>
    <class name=”com.haysmed.hibernate.Team” table=”TEAM” schema=”PP”>
    <id name=”id” type=”java.lang.Long”>
    <column name=”ID” precision=”22″ scale=”0″ />
    <generator class=”sequence”>
    <param name=”sequence”>team_pid</param>
    </generator>
    </id>

    ….

    <set name=”members”>
    <key>
    <column name=”TEAM_ID” precision=”22″ scale=”0″ not-null=”true” />
    </key>
    <one-to-many class=”com.haysmed.hibernate.Member” />
    </set>
    </class>
    </hibernate-mapping>

    Mapping file for Member…

    <hibernate-mapping>
    <class name=”com.haysmed.hibernate.Member” table=”MEMBER” schema=”PP”>
    <id name=”id” type=”java.lang.Long”>
    <column name=”ID” precision=”22″ scale=”0″ />
    <generator class=”sequence”>
    <param name=”sequence”>member_seq</param>
    </generator>
    </id>

    <many-to-one name=”team” class=”com.haysmed.hibernate.Team” fetch=”select” update=”true”>
    <column name=”TEAM_ID” precision=”22″ scale=”0″ />
    </many-to-one>

    … The Search is done by team and the the results (members) are from the getMember() of the team object.

    The struts action that does the update gets the member object from the database using the memberDAO. Could the the problem be with this? I’m going to look into the docs and see what I can uncover.

    #280662 Reply

    Riyad Kalla
    Member

    grrrr… I hate that, when a bug pops back up on you like that.

    Is this 100% reproducable, or is it random? It really does sound like a caching issue… I wonder if you open/close the session before each operation if that will keep the data in sync…

Viewing 9 posts - 1 through 9 (of 9 total)
Reply To: Struts/Hibernate webpage updates are wrong

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