-->
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: Caching with Spring + Hibernate JPA + JBoss Cache
PostPosted: Thu Aug 11, 2011 6:09 am 
Newbie

Joined: Sun Feb 05, 2006 8:56 pm
Posts: 9
I can't seem to make the cache work properly

Here's my persistence.xml


Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
   <persistence-unit name="test.persistence">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      
      <class>com.myapp.datamodel.OrderDetail</class>
      <class>com.myapp.datamodel.OrderDetailAttachment</class>
      <class>com.myapp.datamodel.OrderHeader</class>
      <class>com.myapp.datamodel.OrderHeaderAttachment</class>
      <exclude-unlisted-classes />

      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
         <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
         <property name="hibernate.show_sql" value="true" />
         <property name="hibernate.connection.username" value="......" />
         <property name="hibernate.connection.password" value="........" />
         <property name="hibernate.connection.url" value="........." />
         <property name="hibernate.cache.use_second_level_cache" value="true"/>
         <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.jbc2.MultiplexedJBossCacheRegionFactory" />
      </properties>
   </persistence-unit>
</persistence>



application-context.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
   default-autowire="byName">

   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
      <property name="persistenceUnitName" value="test.persistence" />
   </bean>
   
   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory" />
   </bean>

   <tx:annotation-driven />

   <context:annotation-config />

   <context:component-scan base-package="com.myapp" />
</beans>




Test class

Code:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/application-context.xml")
@Transactional
public class OrderManagerTest {
   
   @Autowired
   private OrderManager orderManager;
   
   @Test
   public void test1() {
      OrderHeader orderHeader = orderManager.getOrderHeader(new BigDecimal(557916160));
   }
   
   @Test
   public void test2() {
      OrderHeader orderHeader = orderManager.getOrderHeader(new BigDecimal(557916160));
   }
}



When running the test, I see in the console the SQL statement executed twice, although I would have expected that the object was already cached.
Thank you


Top
 Profile  
 
 Post subject: Re: Caching with Spring + Hibernate JPA + JBoss Cache
PostPosted: Thu Aug 11, 2011 8:59 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
did you enable caching on the entity?

"use_second_level_cache" is not a global "all on" flag, it's only enabling the capability (so you can easily disable it globally in case you suspect a caching issue), you still have to enable it explicitly on the entities you want to cache, with per-entity configuration.

Same for queries: you have to explicitly enable it on named queries, AND enable to global flag for queries:
hibernate.cache.use_query_cache true

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#performance-cache

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


Top
 Profile  
 
 Post subject: Re: Caching with Spring + Hibernate JPA + JBoss Cache
PostPosted: Thu Aug 11, 2011 10:54 am 
Newbie

Joined: Sun Feb 05, 2006 8:56 pm
Posts: 9
Yes, I have already declared the objects as cacheable:

Code:
@Entity
@Table(name="order_header")
@Cacheable
public class OrderHeader implements Serializable {
.....


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.