-->
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.  [ 2 posts ] 
Author Message
 Post subject: Spring, Hibernate, Ehcache oh my! Help!
PostPosted: Fri Nov 11, 2011 8:20 am 
Newbie

Joined: Wed Feb 02, 2011 5:18 pm
Posts: 3
Hi All,

New to the forum. I've been on a project rewriting a bioinformatics web site using Spring 3.0.5, Hibernate 3.3.2 and Ehcache 2.4.6. We are getting to a load testing stage and want to integrate ehcache. I think I have everything configured properly to get ehcache running. I see ehcache read its config and create the appropriate caches. I see debug messages as domain objects are hit telling me if it was a cache hit or miss is much worse then no 2nd level cache. All looks good, except...... When I watch the database log I still see the queries run, and the performance on what is supposedly the cache hit is horrible. At this point I am not sure where I an going wrong. I suspect a config/annotation problem, but darned if I can find it.

Oh, it may be important to mention a little of our architecture..... We have specialized Solr indexes for user queries. These return object keys which then get instantiated via session.get(Marker.class, id). We are happy with the performance. So adding ehcache is frosting. But I have little time left to figure out why it is making the performance worse, and still hitting the db.

On to some of the config.... I would greatly appreciate if someone could take a look and point out any error.

Thanks in advance,
MArk

my appContext-hibernate.xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:util="http://www.springframework.org/schema/util"
      xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="
         http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

   <!-- ===================== RESOURCE DEFINITIONS ===================== -->

   <!-- Configurer that replaces ${...} placeholders with values from a properties file -->
   <!-- (in this case, JDBC-related settings for the dataSource definition below) -->
   <context:property-placeholder location="classpath:../properties/fewi.properties" />

   <!-- JNDI DataSource for JEE environments -->
   <jee:jndi-lookup id="dataSource" jndi-name="${jndi.datasource}"/>

   <!-- Hibernate SessionFactory -->
   <bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
      p:dataSource-ref="dataSource">
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.generate_statistics">
               ${hibernate.generate_statistics}
            </prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.region.factory_class">
               net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
            </prop>   
            <prop key="hibernate.cache.provider_class">
               org.hibernate.cache.EhCacheProvider
            </prop>      
         </props>
      </property>
      <property name="packagesToScan">
         <list>
            <value>foo.frontend.datamodel</value>
         </list>
      </property>
   </bean>

   <bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"
      p:sessionFactory-ref="sessionFactory"/>
         
   <tx:annotation-driven/>
   
   <!--
   Activates various annotations to be detected in bean classes:
   Spring's @Required and @Autowired, as well as JSR 250's @Resource.
   -->
   <context:annotation-config/>
</beans>


My ehcache.xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>

   <defaultCache eternal="true" maxElementsInMemory="10000" overflowToDisk="false" statistics="true"/>
   <cache
      name="foo.frontend.datamodel.Marker"
      maxElementsInMemory="10000"
      eternal="true"
      overflowToDisk="false"
      statistics="true" />
</ehcache>


and a snippet from my Marker object.....

Code:
@Entity
@Table(name="marker")
@SecondaryTables (
      { @SecondaryTable (name="marker_counts", pkJoinColumns= {
         @PrimaryKeyJoinColumn(name="marker_key", referencedColumnName="marker_key") } )
      } )
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region="mgi.frontend.datamodel.Marker")
public class Marker {

   private List<MarkerAlias> aliases;
   private List<MarkerAlleleAssociation> alleleAssociations;
   private List<BatchMarkerAllele> batchMarkerAlleles;

        ............................ truncated for brevity........
   
    // ================= Instance Methods ===================== //
   
   /** used to make other convenience methods to extract only a certain types
    * of annotations from the full List of annotations
    */
   /**
    * Returns a collection of marker annotation objects, which
    * extend the base annotation class.
    */
    @OneToMany
    @JoinTable (name="marker_to_annotation",
            joinColumns=@JoinColumn(name="marker_key"),
            inverseJoinColumns=@JoinColumn(name="annotation_key")
            )
    @OrderBy("dagName, term")
    @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
    public List<Annotation> getAnnotations() {
      return annotations;
   }


Top
 Profile  
 
 Post subject: Re: Spring, Hibernate, Ehcache oh my! Help!
PostPosted: Fri Nov 11, 2011 10:17 am 
Newbie

Joined: Wed Feb 02, 2011 5:18 pm
Posts: 3
Well this always seems to happen. I hit a wall. Finally give up and compose a post to a forum somewhere. Then I find the solution. So, for posterity, here it is. I hope it helps someone else someday. If anyone sees problems in the original code though, please feel free to make suggestions for improvements.

So, I had only annotated the Marker class for caching. I thought annotating the Collection methods turned on the caching for those objects. Not so. It caches the keys to the elements of the collection. If those objects are not annotated to be cached, they will still require db access. It appears that these queries are slower when the keys are pulled from cache. My take is that the non-cache query is get me all X with a Y key of 123 in non-cache land. But once the keys are in cache, it becomes a separate query for each Y.

Anyhow, our domain model is fairly complex, and annotating only the top object was bad.

Mark


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