-->
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.  [ 23 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: second level caching doubt abt read/write strategy.
PostPosted: Mon Aug 02, 2004 5:46 am 
Newbie

Joined: Fri Nov 28, 2003 10:38 am
Posts: 19
When using read/write strategy are the objects(business objects/entity objects) cached in the session factory? In the code below it does not seem to use the cache and hits the DB each time the code tries to load it from a new session. So does that mean that if you use read/write strategy no second level caching is provided?

Note:
1. if i use read-only & nonstrict-read-write the caching seems to work and does not hit the DB on subsequent loads.
2. I went through the documents, forum and was not able to get enough information to understand why it was not caching in read/write strategy.
3. i do not want to cache the query, i want to cache the objects that are already loaded to reduce the db hits.

mapping file :
Code:
<hibernate-mapping>
    <class name="uk.co.ReviewImpl" proxy="uk.co.idbs.product.Review" table="pd_reviews">
        <cache usage="read-write"/>
        <id name="id" column="id" type="string" unsaved-value="null">
            <generator class="uuid.hex"/>
        </id>
        <version name="auditVersion"/>

        <property name="reviewer" type="string"/>
        <property name="score" type="integer"/>
        <property name="comment" type="string" column="cment"/>
        <property name="buyAgain" type="yes_no"/>
    </class>
</hibernate-mapping>



code:

Code:
sessionFactory = hibernateConfiguration.buildSessionFactory();
Session session = null;
  session = sessionFactory.openSession();
  Object obj1 = session.load(....)
  session.close();

// here i was expecting it to get from cache instead of loading it from DB. But it is loading from DB
  session = sessionFactory.openSession();
  Object obj2 = session.load(....)
  session.close();


I am using net.sf.ehcache.hibernate.Provider


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 5:47 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Why not enable logging for net.sf.hibernate.cache.* and see whats going on?

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 6:19 am 
Newbie

Joined: Fri Nov 28, 2003 10:38 am
Posts: 19
In frist session
Code:
DEBUG: Cache lookup: 8a94b2fcfa346b2200fa3473d6360006
DEBUG: Cache miss: 8a94b2fcfa346b2200fa3473d6360006
DEBUG: Caching: 8a94b2fcfa346b2200fa3473d6360006
DEBUG: Cached: 8a94b2fcfa346b2200fa3473d6360006
Hibernate: select reviewimpl0_.id as id0 from rajtest.pd_reviews reviewimpl0_ where reviewimpl0_.id=?


In the next session
Code:
DEBUG: Cache lookup: 8a94b2fcfa346b2200fa3473d6360006
DEBUG: Cached item was locked: 8a94b2fcfa346b2200fa3473d6360006
DEBUG: Caching: 8a94b2fcfa346b2200fa3473d6360006
DEBUG: Item was already cached: 8a94b2fcfa346b2200fa3473d6360006
Hibernate: select reviewimpl0_.id as id0 from rajtest.pd_reviews reviewimpl0_ where reviewimpl0_.id=?


Looks like it is caching and also accessing from cache. But it does hit the db also(issues a hiberanate query to hit the db).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 6:21 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Well, thats because it was locked, as the log says. Try using Transactions as documented.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 9:54 am 
Newbie

Joined: Fri Nov 28, 2003 10:38 am
Posts: 19
well I am using transcation as recommended. And using LockMode.NONE when loading the object. And i am just loading the object in both the session, no updates are done in this example.

session = sessionFactory.openSession();
transaction = session.beginTransaction();
Object obj1 = session.load(....)
transaction.commit();
session.close();

I also stepped through hibernate second level caching. I am not sure but the obersvation could be a potential bug.

1. When a object is loaded for the first time(read-write strategy) ReadWriteCache.Item is cached with the object we try to load and curent timestamp(System.currentTimeMillis()).

2. when the object is subsequently loaded(in a different transcation), the method call lockable.isGettable(txTimestamp)(line 75 ReadWriteCache) always passes Long.minValue(-9223.......). And the method isGettable(..) compares the Long.minValue & previously cached timestamp and will always return false. Hence we get the log message "Cached item was locked"....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 10:16 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
It still must be your fault, because this works just fine if used correctly.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 1:08 pm 
Newbie

Joined: Fri Nov 28, 2003 10:38 am
Posts: 19
If i use openSession() to get a Session my code works fine. (caching works fine).

But in the case i posted before i am calling
openSession(Connection connection, Interceptor interceptor)

SessionFactoryImpl sets timestamp to Long.minValue if openSession(Connection connection, Interceptor interceptor) (line 316) is called

SessionFactoryImpl sets timestamp to settings.getCacheProvider().nextTimestamp() value if openSession() (line 316) is called

Is there any setting i have to change to get the caching working in my case?

I really appreciate the prompt replies.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 1:10 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Caching will be turned off if you supply your own JDBC connection. Hibernate can't know what else you might have done on this connection, hence you would run get stale data problems.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 1:30 pm 
Newbie

Joined: Fri Nov 28, 2003 10:38 am
Posts: 19
In a way it is the connection got from Hibernate(not a self created jdbc connection). The code actually gets the connection from ConnectionProvider and passes it on to the openSession(....) method. Once the session is closed the connection is returned to the pool. Is that not good enough to use the caching facility?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 1:33 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No. It's really simple: if you call openSession(yourConnection), Hibernate assumes that you have been executing SQL on this connection and that the current database transaction (which is in this case also your responsibility) can not be trusted for the second-level cache. There is no workaround. Let Hibernate manage the connection if you want the second-level cache. You can of course call session.connection() once it is open, do some SQL/JDBC, and then continue. It is then your responsibility to evict() the SessionFactory/second-level cache appropriately if you modified data.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 3:20 am 
Newbie

Joined: Fri Nov 28, 2003 10:38 am
Posts: 19
got it. Thanks for being patient and really appreciate the help provided.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 9:41 am 
Newbie

Joined: Fri Nov 28, 2003 10:38 am
Posts: 19
Due to certain design choices and patterns we use it becomes very necessary to use openSession(connection...), but at the same time we neither perform any SQL/JDBC operation with the jdbc connection nor open any transcation hence in this particular case the connection can be trusted.

Is it possible to have this choice of trust as configurable or to be left to the user?

e.g. by providing a method like

openSession(Connection c, Interceptor i, boolean trustedConnection)
openSession(Connection c, boolean trustedConnection)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 9:42 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No, better implement your own ConnectionProvider.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 9:50 am 
Newbie

Joined: Fri Nov 28, 2003 10:38 am
Posts: 19
We have subclassed the ConnectionProvider to give our implementation, but i cannot understand how this will change the caching choice.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 9:51 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Did you try it or do you want to keep me busy guessing?

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 23 posts ]  Go to page 1, 2  Next

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.