-->
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.  [ 7 posts ] 
Author Message
 Post subject: New SessionFactory required to see external updates?
PostPosted: Mon Nov 07, 2005 7:05 pm 
Newbie

Joined: Mon Nov 07, 2005 6:03 pm
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.x

Mapping documents: 3

Code between sessionFactory.openSession() and session.close():
Code:
// uncommenting this line gets new records
//this.articleSessionFactory = this.buildSessionFactory();
Session session = this.articleSessionFactory.openSession();
try {
    DateFormat dateFormat = DateFormat.getDateTimeInstance(
    DateFormat.SHORT, DateFormat.LONG);
    Date timeStamp = dateFormat.parse(this.getConfiguration()
        .getString(IssueSynchronization.SYNCH_TIMESTAMP));
    Query namedQuery = session.getNamedQuery(this.getConfiguration()
        .getString(IssueSynchronization.HIBERNATE_QUERY));
    namedQuery.setTimestamp("created", timeStamp);
    List list = namedQuery.list();
    String formattedTimeStamp = dateFormat.format(timeStamp);
    // code to update timestamp elided
    IssueSynchronization.LOG.info(MessageFormat
        .format("Read {0} articles published since {1}",
        new Object[] { Integer.toString(list.size()), formattedTimeStamp }));
} catch (Throwable t) {
    throw new SynchronizationException(t);
} finally {
    session.close();
}


Undoubtedly this is something a noob like me is not "getting" - but after looking at docs, posts, etc. I still cannot figure this out: The query results only include new records (inserts from a separate web app) if when a new SessionFactory is created. Without that ugliness an empty list is returned (no new inserts found). This should clue me into what is going on...but unfortunately I'm at a loss right now.

Any insight is appreciated. Btw, I have not deviated for default configuration (of hibernate properties, ehcache, etc.) - in fact I have not even deployed an ehcache.xml.


Top
 Profile  
 
 Post subject: CacheMode
PostPosted: Mon Nov 07, 2005 7:15 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I can't tell you why Hibernate seems to be ignoring new rows inserted since the SessionFactory was created, but I can give you a workaround. Before getting the query, set your Session's CacheMode to REFRESH, get/run the query, then get the CacheMode back to NORMAL.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 7:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
have you enabled second level caching?


Top
 Profile  
 
 Post subject: Cache is not (intentionally) enabled
PostPosted: Tue Nov 08, 2005 11:41 am 
Newbie

Joined: Mon Nov 07, 2005 6:03 pm
Posts: 4
- ehcache.xml not in classpath
- hibernate configuration does not include any *hibernate.cache* properties...in fact i searched all properties files in classpath.


Top
 Profile  
 
 Post subject: Something must be unusual then...
PostPosted: Tue Nov 08, 2005 12:01 pm 
Newbie

Joined: Mon Nov 07, 2005 6:03 pm
Posts: 4
...because the workaround (toggling CacheMode) did not work.

There is this log entry (speaking of cache) that I am going to look into now - but again, my understanding is that I would have to configure Hibernate to second-level cache, regardless of ehcache configuration.

Code:
WARNING: No configuration found. Configuring ehcache from ehcache-failsafe.xml f
ound in the classpath: file:/C:/Program Files/Apache Software Foundation/Tomcat
5.0/work/Catalina/localhost/jira/loader/ehcache-failsafe.xml


Top
 Profile  
 
 Post subject: Querying within a transaction works around the problem.
PostPosted: Tue Nov 08, 2005 5:26 pm 
Newbie

Joined: Mon Nov 07, 2005 6:03 pm
Posts: 4
I altered the code to use a transaction:

Code:
    // code elided
    Transaction transaction = session.beginTransaction();
    // query elided
    transaction.close();
    // clean up elided


Now the query returns externally committed rows - as desired. This is a work-around, but I am obviously curious about the initial root cause. I don't mind - in fact prefer - doing the homework on this...so keeping in mind that cache has not been explicitly enlisted: what are the various things I should check?

As you can see this was as simple as you can get (for external named queries). Considering that no related configuration defaults have been altered, is it safe to say that a Transaction must be enlisted to get read_committed behavior?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 7:28 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
read commited behaviour is set in database - you have same behaviour without hibernate (except if you change database settings)
for the most cases, you want that your query be consistent in transaction or you have dirty read


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