设置hibernate二级缓存

时间:2011-03-03 13:43:23

标签: hibernate spring second-level-cache

我是hibernate和spring的新手,我尝试使用hibernate二级缓存。但它似乎不起作用。我有一个以下测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@TransactionConfiguration
@Transactional
public class CacheTest extends AbstractTransactionalJUnit4SpringContextTests
{   
    @Test
        public void testCache1() 
        {
        System.out.println("Running testCache1");
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        MutableDAO<AppUser> appUserDAO = new MutableDAOImpl<AppUser>(AppUser.class, (SessionFactory) ctx.getBean("OnMediaSessionFactory"), 10);
        assertNotNull("AppUser DAO is null.", appUserDAO);

        SessionFactory sessionFactory = (SessionFactory)ctx.getBean("OnMediaSessionFactory");
        long numberOfUsers = appUserDAO.countAll();

        System.out.println("Number of rows :" + numberOfUsers);
        final String cacheRegion = AppUser.class.getCanonicalName();

        SecondLevelCacheStatistics settingsStatistics = sessionFactory.getStatistics().
            getSecondLevelCacheStatistics(cacheRegion);
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        appUserDAO.findAll();
        stopWatch.stop();
        System.out.println("Query time : " + stopWatch.getTotalTimeSeconds());
        System.out.println(settingsStatistics);
     }

    @Test
    public void testCache2() 
    {
        System.out.println("Running testCache2");
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        MutableDAO<AppUser> appUserDAO = new MutableDAOImpl<AppUser>(AppUser.class, (SessionFactory) ctx.getBean("OnMediaSessionFactory"), 10);
        assertNotNull("AppUser DAO is null.", appUserDAO);

        SessionFactory sessionFactory = (SessionFactory)ctx.getBean("OnMediaSessionFactory");
        long numberOfUsers = appUserDAO.countAll();

        System.out.println("Number of rows :" + numberOfUsers);
        final String cacheRegion = AppUser.class.getCanonicalName();

        SecondLevelCacheStatistics settingsStatistics = sessionFactory.getStatistics().
            getSecondLevelCacheStatistics(cacheRegion);
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        appUserDAO.findAll();
        stopWatch.stop();
        System.out.println("Query time : " + stopWatch.getTotalTimeSeconds());
        System.out.println(settingsStatistics);
     }
}

我有

<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.use_structured_entries">true</prop>

但我输出如下:

Running testCache1
Number of rows :81
Query time : 0.129
SecondLevelCacheStatistics[hitCount=0,missCount=0,putCount=81,elementCountInMemory=81,elementCountOnDisk=0,sizeInMemory=219634]
Running testCache2
Number of rows :81
Query time : 0.063
SecondLevelCacheStatistics[hitCount=0,missCount=0,putCount=81,elementCountInMemory=81,elementCountOnDisk=0,sizeInMemory=219634]

我需要做些什么才能让它发挥作用?

2 个答案:

答案 0 :(得分:3)

您的测试看起来很奇怪,您为每个测试创建了一个新的应用程序上下文,因此Hibernate SessionFactory无法在测试之间以及二级缓存中存活。

正确的测试看起来像这样:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class CacheTest extends AbstractTransactionalJUnit4SpringContextTests
{   
    @Autowired
    private MutableDAO<AppUser> appUserDAO;

    @Autowired
    private SessionFactory sessionFactory;

    private TransactionTemplate tx;

    @Autowired
    public void setPtm(PlatformTransactionManagement ptm) {
        tx = new TransactionTemplate(ptm);
    }

    @Test
    public void doTestCache() {
        // Using programmatic transaction management since we need 2 transactions
        // inside the same method

        // 1st attempt
        tx.execute(new TransactionCallbackWithoutResult() {
            public void doInTransactionWithoutResult(TransactionStatus status) {
                testCache();
            }
        });

        // 2nd attempt
        tx.execute(new TransactionCallbackWithoutResult() {
            public void doInTransactionWithoutResult(TransactionStatus status) {
                testCache();
            }
        });

    }

    public void testCache() {
        long numberOfUsers = appUserDAO.countAll();

        System.out.println("Number of rows :" + numberOfUsers);
        final String cacheRegion = AppUser.class.getCanonicalName();

        SecondLevelCacheStatistics settingsStatistics = sessionFactory.getStatistics().
            getSecondLevelCacheStatistics(cacheRegion);
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        appUserDAO.findAll();
        stopWatch.stop();
        System.out.println("Query time : " + stopWatch.getTotalTimeSeconds());
        System.out.println(settingsStatistics);
     }      
 }

答案 1 :(得分:2)

首先,请记住,Hibernate默认情况下不使用任何缓存提供程序。因此,您需要一个用于Hibernate的2L缓存的“外部”缓存提供程序。对于我的回答,我将使用ehcache和Hibernate 3.3。请注意,在更新版本的Hibernate中更改了配置,因此,请阅读您正在使用的确切版本的文档。

在你的Hibernate配置中,你错过了一个部分,即将Hibernate指向实际的提供者。属性hibernate.cache.provider_class为Hibernate 3.3执行此操作。将其值设置为net.sf.ehcache.hibernate.SingletonEhCacheProvider

现在,您还需要一个ehcache.xml,如下所示:

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

    <diskStore path="./cache" />

    <defaultCache   maxElementsInMemory="10000" 
                    eternal="true"
                    overflowToDisk="true"
                    diskPersistent="true" 
                    diskExpiryThreadIntervalSeconds="120"
                    memoryStoreEvictionPolicy="FIFO" />

    <cache  name="com.mycompany.jpa.MyEntity" 
            maxElementsInMemory="50" 
            overflowToDisk="true" />

    <cache  name="org.hibernate.cache.StandardQueryCache" 
            maxElementsInMemory="50" 
            overflowToDisk="true" />

    <cache  name="org.hibernate.cache.UpdateTimestampsCache" 
            maxElementsInMemory="5000"
            overflowToDisk="true" />

</ehcache>

你没有展示你的DAO,所以,我不确定它是否正确。请注意,您始终需要明确缓存,因为它意味着使用特定位置的解决方案,而不是一切的通用解决方案。这意味着,在您的DAO中,您将添加一个查询提示,指出您的查询是可缓存的(从您的测试中,您似乎需要查询缓存,而不仅仅是实体缓存)。

如果仍然无法使其正常工作,请参阅以下JIRA中的附件。它包含一个启用了缓存的maven项目,因此,您可以将其与您的代码进行比较:

https://issues.jboss.org/browse/JBPAPP-4224