-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Database and thread of execution synchronization
PostPosted: Mon Jun 21, 2004 12:46 pm 
Beginner
Beginner

Joined: Mon Feb 09, 2004 3:15 pm
Posts: 34
I have an interesting problem with the code below. What happens is that this method finishes execution prior to the database finishing its work. So what ends up happening is that later in my thread of execution i am querying the DB (with a new sesssion) for the newly inserted rows, however, the DB is still working on the deleting of the old ones and inserting of the new rows. And i end up getting a weird mix of data.

The problem i am trying to solve is how to force the following statements to finish execution (including the DB work) before returning control to the primary thread of execution in the VM.

Any ideas would be greatly appreciated.

Code:
        // delete existing selections
        session.delete("some-query-that-returns-the-objects-i-want-to-delete);

        // insert new selections
        Iterator answers = myAnswers.iterator();
        while(answers.hasNext()) {
            Answer answer = (Answer) answers.next();
            if(answer != null && !answer.getAnswer().equals("")) {
                session.save(answer);
            }
        }


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 21, 2004 1:02 pm 
Beginner
Beginner

Joined: Mon Feb 09, 2004 3:15 pm
Posts: 34
What's happening is that the SQL for inserting the objects is getting executed at the same time as other parts of the application are retrieving data. Ideally i would like all of the inserts to complete, i.e. the while loop to finish executing, before control is returned to the main thread of execution.


Code:
13:00:13,181 INFO  [STDOUT] Hibernate: insert into PSTATUSERSELECTION (USERID, QUESTIONID, ANSWERID, DATEINSERTED) values (?, ?, ?, ?)

13:00:13,197 INFO  [STDOUT] Hibernate: select useravatar0_.USERID as USERID0_, useravatar0_.AVATAR as AVATAR0_ from USERPROFILE useravatar0_ where useravatar0_.USERID=?
13:00:13,213 INFO  [STDOUT] Hibernate: select usertaglin0_.USERID as USERID0_, usertaglin0_.TAGLINE as TAGLINE0_ from USERTAGLINE usertaglin0_ where usertaglin0_.USERID=?

13:00:13,213 INFO  [STDOUT] Hibernate: insert into PSTATUSERSELECTION (USERID, QUESTIONID, ANSWERID, DATEINSERTED) values (?, ?, ?, ?)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 21, 2004 3:01 pm 
Beginner
Beginner

Joined: Mon Feb 09, 2004 3:15 pm
Posts: 34
Maybe some more details will help.

At a high level, this what i'm trying to accomplish:

Code:
// save user selections
profileDelegate.setUserInterestsAnswers(userId, answers);

// forward to view interests page
return new ActionForward("interests", "/profile/interests/index.jsp?membername=" + request.getParameter("membername"), true, true);


1. Save answers
2. Forward to a new page to view answers


What looks like is happening is that the setUserInterestsAnswers() method, which ultimately ends up calling the session.delete() and session.save() methods from the first post, returns control of execution before the database has finished inserting the data. The reason i know this is that when i look at my server log, there are insert SQL statements from the session.save() method call interspersed with SQL statements that retrieve data for the page that is getting forwarded to.

Now, if you follow the code, you'll see that the session.delete() and session.save() calls are made prior to forwarding to the new page, but in execution, it looks like they are happening in parallel.

Does hibernate spawn threads for inserts that run in parallel with the main execution thread???


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 21, 2004 5:10 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
// delete existing selections
session.delete("some-query-that-returns-the-objects-i-want-to-delete);
session.flush();
// insert new selections
Iterator answers = myAnswers.iterator();
while(answers.hasNext()) {
Answer answer = (Answer) answers.next();
if(answer != null && !answer.getAnswer().equals("")) {
session.save(answer);
}
}

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 22, 2004 2:35 pm 
Beginner
Beginner

Joined: Mon Feb 09, 2004 3:15 pm
Posts: 34
I am officially a part-time idiot :)

The reason it was querying the table that i was updating was because of the following html tag...

<a href="some.jsp" onclick="submitMyForm();">accept changes</a>

what happened was that when you clicked on the 'accept changes' link, it simultaneously retrieved the jsp to view the answers and submitted the form via struts to update the answers.

solution was to replace the href value with a "#" character so that a new page request is not generated...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.