-->
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: Read-write cache not reading external database modifications
PostPosted: Sat May 14, 2005 3:10 pm 
Newbie

Joined: Fri Mar 19, 2004 7:06 pm
Posts: 6
Hibernate version: 2.1.8

Name and version of the database you are using: MySQL 4.1

Mapping documents:
Code:
<class name="Attribute" table="ec_attribute">
  <cache usage="read-write"/>
  <id name="attributeID" type="integer" unsaved-value="null" >
      <column name="attribute_id" sql-type="int" not-null="true"/>
         <generator class="native"/>
   </id>
   <property name="name"/>
   <property name="value"/>
</class>



Cache Provider: OSCache 2.1.1

I am using read-write second-level caching using OSCache and everything works fantastic except when I modify the database outside of the application. If I take out the <cache usage="read-write"/> from my POJO mappings, and then make direct changes to the database, the application picks them up immediately. The problem is when I have read-write cache turned on, it won't see the external database changes done from another web application or command line mysql executions.

The one thing I haven't yet tried to do is evict the specific object that needs to be refreshed. I haven't done this yet because at any given time I don't know which object needs to be evicted... it could be any of them depending on which table and column was updated within the database. I would expect the read-write caching mechanism to identify this anyway.

Any help will be appreciated. I love hibernate and think it is an outstanding application... and I am hoping that this is a user error that I can quickly correct!

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 14, 2005 3:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
If you don't know which object needs evicting, evict the whole cache region. Look at the javadoc for SessionFactory.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 14, 2005 6:30 pm 
Newbie

Joined: Fri Mar 19, 2004 7:06 pm
Posts: 6
Thanks Gavin for responding.

Okay, since caching is done by region, and a region turns out to simply be the class name, I wrote the following code to entirely clear my cache.

Code:
Map classMap = factory.getAllClassMetadata();            
Collection values = classMap.values();
Iterator iter = values.iterator();      
while (iter.hasNext()) {
   ClassMetadata meta = (ClassMetadata)iter.next();
   factory.evict(meta.getMappedClass());
}



I am still curious as to why read-write cache doesn't pick up on the changes made to the database by outside applications. I am assuming it is suppose to be able to handle that, correct? I've tested out EHCache and OSCache to see if hibernate behaves differently and it doesn't, so I am guess there is something internal to hibernate that I have configured that is not allowing hibernate to see new changes to the DB from outside sources.

Does it matter that I am not using JNDI? When hibernate initializes within my Tomcat environment, I always see the log "No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)".

I am going to search through the source to see if I can understand this for myself, but any insight you have would help.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 14, 2005 7:03 pm 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
Think about it, if you want the 2nd level cache to pick up changes to your tables as soon as you make them externally, it will need to query the database on every cache request. Er, then it wouldn't be a cache.

The cache will not pick up changes made directly to the database outside of your application. It is not supposed to - that is how caches work.

For the tables you need to update externally, try configuring those regions in your chosen cache provider with a low cache ttl. Perhaps that will be enough for your requriements.

Or if you must force cache refresh immediatley, send a request to a simple servlet or JMX component to clear the appropriate cache region or better use CacheMode.REFRESH for a specific query.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 14, 2005 7:28 pm 
Newbie

Joined: Fri Mar 19, 2004 7:06 pm
Posts: 6
Makes sense. I have been pleasantly suprised with a lot of things within hibernate, so I figured they had some special voodoo magic they did to see external database updates. I just couldn't confirm the existence or non-existence of this within any print or forum posts.

All is well then. Thanks for the pointer gavin, and thanks for making the obvious clear to me beniji. ;)


Top
 Profile  
 
 Post subject: Re: Read-write cache not reading external database modifications
PostPosted: Fri Feb 12, 2010 4:51 pm 
Newbie

Joined: Fri Feb 12, 2010 4:44 pm
Posts: 1
Hibernate version: 3

Name and version of the database you are using: MySQL 5.1

Mapping documents:
annotations in class

Cache Provider: default

Hi.
i have a similiar question:
i have a servlet app,many open sessions - for each thread there is 1 session
1st question:
is it possible to invalidate cache or force a refresh on objects which were written from one session to the others?
i have tried to do it like this in the function refreshobject
Code:
public static class HibernateUtil {
      public static HashMap<Long,Session> sessions=new HashMap<Long,Session>() ;
//      static final ThreadLocal<Session> local = new ThreadLocal<Session>();
      public static SessionFactory sessionFactory;
//      static Vector<Session> sessions=new Vector<Session>();
      public static Session currentSession() throws HibernateException
      {
         Long threadid=Thread.currentThread().getId();
         
         Session session = sessions.get(threadid);
         //open a new session if this thread has no session
         if(session == null) {
            session = sessionFactory.openSession();
            sessions.put(threadid,session);
         }
         return session;
      }
      public static void refreshSessions()
      {
         for(Entry<Long,Session> e:sessions.entrySet())
         {
            e.getValue().disconnect();
            e.setValue(sessionFactory.openSession());
         }
      }
//      public static void refreshObject(Objekt o)
//      {
//         if(o!=null)
//            for(Entry<Long,Session> e:sessions.entrySet())
//               e.getValue().refresh(o);
//      }
      
   }


currently i am using refresh sessions but i would like to use the refreshObject function which is more efficient.
the intention of the refreshObject is to call refreshObject after save().
2nd question:
can you tell my why refreshObject isn't working ?
thanks.
Mike


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.