-->
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.  [ 8 posts ] 
Author Message
 Post subject: [ehcache + hibernate] Cache doesn't work ...
PostPosted: Mon Jun 12, 2006 7:42 am 
Beginner
Beginner

Joined: Fri Sep 16, 2005 7:51 am
Posts: 25
I to all.


I have a simple problem with some [ehcache + hibernate] application.


Basically, I have two classes


@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public abstract class Item extends BaseObject implements Serializable{
(...)


and

@Entity
public class SimpleItem extends Item implements Serializable{
(...)


as you can see, one is abstract and the other is concrete. My application only
deals with concrete one, of course.


I have a simple DAO for managing SimpleItem instances.


My configuration is ok : I can save, read, update (aso) SimpleItems.
I have an Open Session in view filter in place for managing transactions.
Everything work smoothly as I expected.


I now want to enable some caching for those entities.

I plugged ehcache, added configuration in my entities (as you saw above), configured ehcache
and hopped things will work...


Here is the usual suspects :


hibernate.cfg.file
------------------
<hibernate-configuration>
<session-factory>
<mapping class="system.model.BaseObject" />
<mapping class="system.model.entities.Item" />
<mapping class="system.model.entities.SimpleItem" />
</session-factory>
</hibernate-configuration>


spring hibernate configuration file
------------------
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="dataSource"/></property>
<property name="configLocation"><value>/WEB-INF/hibernate.cfg.xml</value></property>
<property name="configurationClass"><value>org.hibernate.cfg.AnnotationConfiguration</value></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
</bean>

ehcache.xml (note : dummy copy/paste from the web...)
---------------
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir"/>

<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>


<cache name="sampleCache1"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
/>

</ehcache>



When I startup my application, I have the following traces :


2006-06-12 12:16:25,234 INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
2006-06-12 12:16:25,234 INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
2006-06-12 12:16:25,234 INFO org.hibernate.cfg.SettingsFactory - Cache provider: org.hibernate.cache.EhCacheProvider
(...)
2006-06-12 12:16:26,843 WARN org.hibernate.cache.CacheFactory - read-only cache configured for mutable class:
system.model.entities.Item
2006-06-12 12:16:26,843 WARN org.hibernate.cache.EhCacheProvider - Could not find configuration [
system.model.entities.Item]; using defaults.


Guess what my problem is ?

The cache doesn't work !

Hibernate always trigger some SQL when I try to fetch SimpleItem instances.

I highly suspect a configuration problem, but didn't succeeded finding a satysfiying answer
in all the documentation I read.
The second warning must probably lead my to a solution, but I must confess that I'm running in circle...

I'm highly disapointed and beg all for some help, enlightenment and salvation ! ;)

thanks in advance,

Sne.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 8:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
goto user forum. this is tools related.


...and remember that "enable caching" != no sql

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 8:12 am 
Beginner
Beginner

Joined: Fri Sep 16, 2005 7:51 am
Posts: 25
Ok, sorry for the wrong place post ! ;)

Ok for ["enable caching" != no sql] => but is this possible with a simple select all ?

but i checked objects references too (hashcode) and they were different each time I load the List.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 8:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
of course they are different - otherwise the objects would be shared between sessions and you would have concurrency issues etc.

i recommend reading up on how second level cache actually works.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 8:39 am 
Beginner
Beginner

Joined: Fri Sep 16, 2005 7:51 am
Posts: 25
Ok ! ;)

I'm new to second level caching and thought I would get it easily... obviously not !

Any good resource you could advise me to read beyond hibernate documentation ?


EDIT :
Ok.
I tried

getSessionFactory().getStatistics()
.getSecondLevelCacheStatistics("system.model.entities.Item")
.getEntries());

before and after a first call proved me that obviously, the cache was filled with objects returned by query.

So objects are put to the cache.

Something I didn't get is that, after having set the logging level to "debug",
it's OBVIOUS that hibernate make some SQL call.

FIRST CALL :
2006-06-12 15:02:02,453 DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection
2006-06-12 15:02:02,453 DEBUG org.hibernate.SQL - select this_.id as id1_0_, this_1_.name as name1_0_, this_1_.descrip
tion as descript3_1_0_ from SimpleItem this_ inner join Item this_1_ on this_.id=this_1_.id
Hibernate: select this_.id as id1_0_, this_1_.name as name1_0_, this_1_.description as descript3_1_0_ from SimpleItem th
is_ inner join Item this_1_ on this_.id=this_1_.id
2006-06-12 15:02:02,453 DEBUG org.hibernate.jdbc.AbstractBatcher - preparing statement
2006-06-12 15:02:02,468 DEBUG org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, global
ly: 0)
2006-06-12 15:02:02,468 DEBUG org.hibernate.loader.Loader - processing result set
2006-06-12 15:02:02,468 DEBUG org.hibernate.loader.Loader - result set row: 0
2006-06-12 15:02:02,468 DEBUG org.hibernate.type.LongType - returning '1' as column: id1_0_

SECOND :
2006-06-12 15:02:45,890 DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedState
ments: 0, globally: 0)
2006-06-12 15:02:45,890 DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection
2006-06-12 15:02:45,890 DEBUG org.hibernate.SQL - select this_.id as id1_0_, this_1_.name as name1_0_, this_1_.descrip
tion as descript3_1_0_ from SimpleItem this_ inner join Item this_1_ on this_.id=this_1_.id
Hibernate: select this_.id as id1_0_, this_1_.name as name1_0_, this_1_.description as descript3_1_0_ from SimpleItem th
is_ inner join Item this_1_ on this_.id=this_1_.id
2006-06-12 15:02:45,890 DEBUG org.hibernate.jdbc.AbstractBatcher - preparing statement
2006-06-12 15:02:45,890 DEBUG org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, global
ly: 0)
2006-06-12 15:02:45,890 DEBUG org.hibernate.loader.Loader - processing result set
2006-06-12 15:02:45,890 DEBUG org.hibernate.loader.Loader - result set row: 0
2006-06-12 15:02:45,890 DEBUG org.hibernate.type.LongType - returning '1' as column: id1_0_



So what is the probleme here ?
I re-read a part of a book on Hibernate 3 (in french) and it was clearly saying that there will be no sql invovled in a second call if cache is enabled (in the case of a READ-ONLY cached entity)...

Am I missing something ?

Thank you for your time,

Seb.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 11:07 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Hibernate In Action

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 4:35 am 
Beginner
Beginner

Joined: Fri Sep 16, 2005 7:51 am
Posts: 25
Ok. Just re-read the cache section on Hibernate in Action.

Obviously, there is a clear and important distinction between collection caching and instance caching.

But it wasn't clear to me on how a call to a method like
public List loadAll(Class entityClass)
can be cached ?

I can tell ehcache to cache a collection (association or composition for exemple) for a given class, but how can I tell him to cache a loadAll result ?

Must I make use a query cache ???


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 10:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i think you will have much better chance of getting an answer if you ask it in the user forum ;)

_________________
Max
Don't forget to rate


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