-->
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.  [ 3 posts ] 
Author Message
 Post subject: New to Hibernate - can't get caching to work
PostPosted: Tue May 13, 2008 2:59 pm 
Newbie

Joined: Tue May 13, 2008 2:44 pm
Posts: 5
I've been struggling for nearly a day to enable hibernate caching with either OSCache and Ehcache. The caching providers are configured correctly since no exceptions are thrown. My query executes and returns the correct result. The problem is that the query always executes, but I want it to retrieve the result from the cache.

Here is what I have (I've tried not using a session-factory name as well as the EHcache provider):

Code:
    <session-factory name="java:comp/env/hibernate/SessionFactory">

        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property>
name="connection.datasource">a data source</property>

        <property name="current_session_context_class">thread</property>
                                                   
        <property name="cache.provider_class">org.hibernate.cache.OSCacheProvider</property>
        <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.ResinTransactionManagerLookup</property>

        <mapping resource="com/testdesigner/image/ImageHash.hbm.xml"/>

    </session-factory>


Mapping file (I've tried read-only):
Code:
<hibernate-mapping>
    <class name="com.testdesigner.image.ImageHash" table="ImageHashes">
        <cache usage="read-write"/>
        <id name="url" column="imageURL">
            <generator class="assigned"/>
        </id>
        <property name="hashCode" column="imageHashCode"/>
    </class>
</hibernate-mapping>


My Java code:
Code:

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            Transaction tx = session.beginTransaction();
            Query query = session.createQuery("from ImageHash where url = :url");
            query.setString("url", url.toString());
            ImageHash imageHash = (ImageHash) query.uniqueResult();
            tx.commit();
        } finally {
            if (session.isOpen()) session.close();
        }



My log continues to show the hibernate query for each http request.
Code:
Hibernate: select imagehash0_.imageURL as imageURL1_, imagehash0_.imageHashCode as imageHas2_1_ from ImageHashes imagehash0_ where imagehash0_.imageURL=?


Any suggestions on what I should try. Also, I have both ehcache.xml and oscache.properties files in my classpath. I'm using the most recent version of Hibernate core, OSCache and EHCache. They are using the a simple configuration, but I have tried several settings without improvement.

My goal is to cache the objects returned from this query.

Thank you for your help.

Michael


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 13, 2008 5:11 pm 
Newbie

Joined: Tue May 13, 2008 2:44 pm
Posts: 5
I've been doing some reading and it appears that I have to use session.get to use Hibernate's second level cache.

So I changed my code to this:
ImageHash imageHash = (ImageHash) session.get(ImageHash.class, url.toString());


But I still see multiple queries (the same queries as earlier) in the log. Thoughts?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 13, 2008 5:19 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
BacMan wrote:
But I still see multiple queries (the same queries as earlier) in the log. Thoughts?



It also depends how long underlying cache keeps the object. You may want to specify a cache region for the ImageHash class and configure the region to live for long enough. Enabling hibernate stats might also give a sign what is wrong here.



Farzad-


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