Hibernate二级缓存不起作用

时间:2016-03-09 12:01:59

标签: java hibernate second-level-cache

我陷入了困境,无法理解我的二级缓存配置有什么问题。 pom文件

<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.12</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.11.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>4.3.11.Final</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.36</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>4.3.11.Final</version>
    </dependency>
</dependencies>

hibernate.cfg.xml中:

<property name="show_sql">true</property>
 <property name="format_sql">true</property>
 <property name="use_sql_comments">true</property>
 <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
 <property name="hibernate.c3p0.min_size">5</property>
 <property name="hibernate.c3p0.max_size">100</property>
 <property name="hibernate.c3p0.timeout">1800</property>
 <property name="hibernate.c3p0.max_statements">50</property>
 <property name="hibernate.c3p0.idle_test_period">3000</property>   
 <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</property>
 <property name="hibernate.cache.use_second_level_cache">true</property>
 <property name="hibernate.cache.use_query_cache">true</property>
 <property name="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</property> 

ehcache.xml文件

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

 <defaultCache maxElementsInMemory="100" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="200" />    

<cache
    name="version2Cache"
    maxElementsInMemory="100000"
    eternal="true"
    overflowToDisk="false"
    memoryStoreEvictionPolicy="LFU"
    statistics="true"
/>
</ehcache>   

实体:

@Entity
@Table(name = "version")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE,region="version2Cache")
public class VersionData implements Serializable
{

    /**
     * 
     */
    private static final long serialVersionUID = 7355281418627668744L;

    @Id 
    @Column(name="VERSION_ID")  
    private String versionId;



    /**
     * user docname
     */
    @Column(name = "DOC_NAME")
    private String docName; 


    /**
     * user versionModivied
     */
    @Column(name = "VERSION_MODIFIED")
    @Type(type = "timestamp")
    @JsonIgnore
    private Date versionModivied;
    /**
     * user zvitId
     */
    @ManyToOne
    @JoinColumn(name="ZVIT_ID")
    private ZvitData zvitId;

    @OneToMany(targetEntity=TermData.class, mappedBy = "versionId", cascade=javax.persistence.CascadeType.ALL, fetch=FetchType.LAZY)
    private List<TermData> terms;
//getters, setters, hashcode, equals
}

我进行单元测试

@Test
    public void testCache() throws ReportException
    {
        SessionFactory sessionFactory = ReportsFactory.getSessionFactory();
        Session session = sessionFactory.openSession();
        VersionData versionData = (VersionData) session.load(VersionData.class, "1");
        System.out.println(versionData.getDocName());
        session.close();

        session = sessionFactory.openSession();
        versionData = (VersionData) session.load(VersionData.class, "1");
        System.out.println(versionData.getDocName());
        session.close();
    }

我在控制台中看到实际上有两个查询在数据库中运行,但是使用二级缓存时应该有一个。

EDDITED: 谢谢Madhusudana Reddy Sunnapu。我的错误是我没有正确读取控制台消息:( 我没有缓存嵌入的对象。看起来查询工作了两次。但第二次它并没有真正加载VersionData,而是查询了嵌入式对象。而我错过了它。对不起烦恼。这是我真正愚蠢的错误:(

1 个答案:

答案 0 :(得分:0)

您应该添加此内容并重试您的测试。你可能得到了答案。

<ehcache>
     ....
     ....
     //your Code
     .....
     .....
    <cache name="package.Name.VersionData"   
        maxElementsInMemory="100"   
        eternal="false"   
        timeToIdleSeconds="5"   
        timeToLiveSeconds="200" />  
    </ehcache>