-->
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.  [ 9 posts ] 
Author Message
 Post subject: Adding EhCache to JPA and Hibernate project
PostPosted: Sat Sep 13, 2008 5:50 am 
Newbie

Joined: Sat Sep 13, 2008 5:16 am
Posts: 12
Hi,

I'm trying to add EhCache to a JPA and Hibernate project.

Below is my configuration.

persistence.xml:


<persistence-unit name="pollPersistenceUnit">
<properties>
<!-- 2nd level cache -->
<property name="hibernate.cache.provider_class"
value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" />
<property name="hibernate.cache.provider_configuration" value="/ehcache.xml" />
<property name="hibernate.cache.use_second_level_cache"
value="true" />
<property name="hibernate.generate_statistics" value="true" />
<property name="hibernate.cache.use_structured_entries"
value="true" />
</properties>
</persistence-unit>


ehcache.xml:


<ehcache>
<diskStore path="java.io.tmp"/>
<defaultCache
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"/>
<cache name="se.aftonbladet.poll.bean.Poll"
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="180"
overflowToDisk="true"/>
</ehcache>



Poll class annotations:


@Entity
@Table(name = "poll")
@org.hibernate.annotations.Cache(usage =
org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE
)
public class Poll implements Serializable {
...


I'm using Spring and here is my applicationContext.xml:


<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPost
Processor" />
<!-- pollManager and databaseInterface are our own Spring beans (from
Poll project) -->
<bean id="pollService" class="se.aftonbladet.poll.PollServiceManager">
<property name="pollDAO" ref="pollDAO"/>
</bean>

<beanid="pollDAO"class="se.aftonbladet.poll.JPAPollDAO">
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBea
n">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="pollPersistenceUnit" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="false" />
</bean>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="..." />
<property name="username" value="..." />
<property name="password" value="..." />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>



I'm not really sure how to test if the cache is working. What would be the best way to do this?

Another thing I can't figure out is what the READ_WRITE CacheConcurrencyStrategy really means. Can I use EhCache for batch insertion into db? Or is it only used for caching beans read from db?

_________________
/best regards, Håkan Jacobsson


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 13, 2008 8:32 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
just enable some more logging for the correct packages,
you'll see the cache puts/hits/misses...

Code:
<category name="org.hibernate">
      <priority value="ERROR"/>
   </category>

   <category name="org.hibernate.cache">
      <priority value="DEBUG"/>
   </category>

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 13, 2008 10:04 am 
Newbie

Joined: Sat Sep 13, 2008 5:16 am
Posts: 12
Aha, thanx!

Do you also know what the EhCache READ_WRITE configuration does?
I've tried to google it, but I can't seem to understand if EhCache can be used for batch insertion as well as caching beans read from db.

Also, does my configuration so far look OK to you?

_________________
/best regards, Håkan Jacobsson


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 13, 2008 10:18 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
Also, does my configuration so far look OK to you?

looks good, but I don't know Spring, also I can't say about the caching options you selected as every application is different.

Quote:
I can't seem to understand if EhCache can be used for batch insertion as well as caching beans read from db

what do you mean? when you commit a transaction you want to have the entities flushed to disk, right? so what does a cache have to do with batch writing?

If you have have hibernate begin a transaction, then persist 100 entities, then commit or flush you will get 100 entities pushed to disk anyway, even without a cache.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 13, 2008 11:11 am 
Newbie

Joined: Sat Sep 13, 2008 5:16 am
Posts: 12
Eh, ok I'm even more clueless now.;)

Flushed to disk - what does that mean? Sorry for being such a newbie. I understand that stuff actually gets done (visavi the database) when the transaction is over (or I call commit/flush explicitly).
What I would like to have is batch insertion. I thought maybe Ehcache could help me with this too. But you say that when the transaction is over (or I call commit/flush explicitly), then my entity/entities will be inserted into db - with or without Ehcache? And if I want to batch up a number of entities before actually inserting into database - then I have to make this happen in the code, before starting a new transaction?

/Håkan

_________________
/best regards, Håkan Jacobsson


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 13, 2008 12:19 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
don't worry, you're welcome to ask questions, if it's a bad one nobody will answer;-)

what I meant to say is that, (as far as I know) ehcache (nor any other caching solution) has anything to do with batch inserts.
You achieve the "batch performance" by persisting many (100? 1000? don't push it too much..) during the same transaction.

something like:
tx.begin();
for ( ...) {
persist();
}
tx.commit();

after that you can tweak many more settings, but caching is not really relevant (it could actually slow you down). you'll have to try settings, just remember to not have too big transactions.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 13, 2008 9:07 pm 
Newbie

Joined: Sat Sep 13, 2008 5:16 am
Posts: 12
Much clearer now - thanx a lot for your time and patience!

_________________
/best regards, Håkan Jacobsson


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 14, 2008 5:53 pm 
Newbie

Joined: Sat Sep 13, 2008 5:16 am
Posts: 12
Hi again=)

I have the cache working fine now. Except for one problem.
I'm using query caching as well as second level caching.
I have two beans:

Poll
Answer

Poll has a collection of Answers.
I've added the collection as a cache region in the ehcache.xml.
I've annotated the collection (in the Poll class) with the proper annotation:

@org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)

I've used the setHint-method on the query where I retrive the Poll object and its Collection of answers:

List<Poll> polls = (List<Poll>)entityManager.createQuery("select distinct object(p) from Poll p INNER JOIN FETCH p.answers WHERE p.id = :id").setParameter("id", pollid).setHint(”org.hibernate.cacheable”, true).getSingleResult();


I try to retrieve a Poll object with its Answers twice to test the caching. First call is OK, I get my answers in the Poll object. But the second call fails with a LazyLoadInitialization exception (I'm calling getAnswers() outside of the transaction). The first call gets the Poll object from the database and the second call gets it from the cache.

Do you have an idea as to what may cause this?
(I have some logging of the cacheing and the Collection cache region is found.)

_________________
/best regards, Håkan Jacobsson


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 1:11 pm 
Newbie

Joined: Sat Sep 13, 2008 5:16 am
Posts: 12
Sorry for the typo in the code showing the query. Should'nt be a List of Polls. Just Poll p = (Poll)entityManager...

_________________
/best regards, Håkan Jacobsson


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.