-->
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.  [ 6 posts ] 
Author Message
 Post subject: Problem with hibernate session.createQuery().list()
PostPosted: Mon Jun 02, 2008 6:08 pm 
Newbie

Joined: Mon Jun 02, 2008 5:39 pm
Posts: 2
I have an application running on tomcat 5.5.
It uses struts, tiles, hibernate and MySql 5.0 as backend.

PROBLEM:
Fresh Data is inserted into a table in MySql through hibernate APIs.
Immediately after the insertion, when the table is queried,
fresh data never shows up, until arroximately 10 seconds or so.

Details:
=====
1. Data is inserted as mentined below.

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Obj obj = New Object //Mapped Hibernate class
//Set all the properties of the object
//And then persist to the database
session.persist(obj);
session.flush();
session.getTransaction().commit();

2. Now the object is queried and the returned List is shown in a jsp page using <display:table>.
Please note between these 2 steps, session is closed and opened.

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String query = " from Obj ";
List appResult = session.createQuery(query).list();
return appResult;

I printed out the appresults size and data, it does not contain the fresh insertions.

THE PROBLEM is that, in the page only old data shows up and the fresh insertions do not show up (even after hitting ctrl+refresh).
After approx 10 secs (which varies), the jsp page shows the newly committed data in the page.

If you are familiar with the issue, please help, as I am unable to figure out the problem from past several weeks.

Thanks and Regards
ROY


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 03, 2008 3:54 pm 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
do you close the session after your first commit? you should check maybe by debugging if after the commit everything on your database is updated, if so it's not an hibernate specific problem. i guessing it has to do with you view, maybe there is a cache ??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 12:07 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
I suggest you use session.save() instead of session.persist(). Just try it and let me know if it worked.

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 10:05 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Yes, try session.saveOrUpdate() instead of session.persist(obj);

I might also get rid of the flush, and perhaps do a refresh in the method that's not loading. I'd only add the refresh during debugging, as it shoudln't be needed, but it might be interesting to see if the refresh is bringing in the latest data.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 10:54 am 
Newbie

Joined: Mon Jun 02, 2008 5:39 pm
Posts: 2
By the way, Sukirtha-by any chance r u a Bong-Sylheti. Just wondering seeing ur username. Never Mind :)

Thanks Everyone for thr replies. I am really down with this stupid issue.

I tried with save and saveOrUpdate, but with no success.
Actually after I save the record to the database, immediately after I am able to query the record in MySql using phpMyadmin. Which means that data got into the table. After checking in mysql, when I go and refresh the list in JSP page, it does not show up.

Even this problem exists for
session.load(Mappedclass, keyId);
Hibernate exception is caught with the message that no record with the primary key id is found. But before executing this method, I checked in MySql, the record exists there.

IS IT SOMETHING RELATED WITH THE SESSION USAGE ANYWAY?

What I am doing is
1. Getting the SessionFactory from HibernateUtil class
public class HibernateUtil
{

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

}

2. Getting new sessions for every transaction to the database
try
{
if (this.session!=null) this.session.close();
this.session = HibernateUtil.getSessionFactory().openSession();
}

3. Typically, the insertion of record is done, and the session is flushed and transaction committed. Session is closed.

4. Then for loading the list OR loading the new record from table, a new session is opened.

My only concern is, if there is a delay in commiting the record, I should not be able to see the record using PhpMyAdmin too. And after I am able to see the record in PhpMyAdmin, I should not be getting any HibernateException while loading the new record with key id, AND the list should show me the new record as well. ALL of these happens seamlessly after some time or if I logout and login again or if I restart my server.

Am I doing something wrong in using sessions?

Thanks once again. Will be looking forward for your help.

ROY


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 12:00 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Quote:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String query = " from Obj ";
List appResult = session.createQuery(query).list();
return appResult;


From your previuos post, I get that the quoted text is used for querying. I see a session.beginTransaction() but we do not have a matching counterpart to end the transaction. Could that be an issue??? Do we need a transaction at all in this case?

Quote:
By the way, Sukirtha-by any chance r u a Bong-Sylheti.

No I am not :)

_________________
Sukirtha


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.