java - Hibernate 二级缓存和 JUnit

标签 java hibernate junit ehcache hibernate-cache

我正在尝试将 SecondLevelCache 与 hibernate 一起使用。这是我的 xml 配置文件:

    <persistence-unit name="EntityTestHibernate" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/DB_NAME"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
            <property name="hibernate.connection.username" value="USERNAME"/>
            <property name="hibernate.connection.password" value="PASSWORD"/>            
            <property name="hibernate.cache.use_second_level_cache" value="true"/>             
            <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" /> 
            <property name="hibernate.cache.provider_configuration_file_resource_path" value="/test/ehcache.xml" />     
        </properties>
    </persistence-unit>

我的ehcache.xml:

<ehcache name="cacheTest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
    <defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />
    <cache  name="entityCache" 
            maxEntriesLocalHeap="50"
            eternal="false"
            timeToLiveSeconds="120"         
    />
</ehcache>

在我的实体上有一个这样的注释

@Cache(region="entityCache", usage=CacheConcurrencyStrategy.READ_WRITE )

当我运行 UnitTest 时,出现以下错误(如果我在其上设置或未设置注释 @DirtiesContext,问题是相同的):

net.sf.ehcache.CacheException: Another CacheManager with same name 'cacheTest' already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]
    at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:573)
    at net.sf.ehcache.CacheManager.init(CacheManager.java:389)
    at net.sf.ehcache.CacheManager.<init>(CacheManager.java:371)
    at net.sf.ehcache.hibernate.EhCacheProvider.start(EhCacheProvider.java:93)
    at org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge.start(RegionFactoryCacheProviderBridge.java:72)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:238)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:850)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)

我刚刚使用 hibernate xml 文件配置了我的 CacheManager,但我不知道如何管理该错误。我在某处读到我必须使用 RegionFactory 但在 ehcache 文档中这不是最佳实践。我怎样才能以正确的方式解决我的问题?

最佳答案

不确定您在哪里看到使用 RegionFactory 不是最佳实践...查看 ehcache 文档 http://ehcache.org/documentation/user-guide/hibernate#Configure-Ehcache-as-the-Second-Level-Cache-Provider...and这正是他们所说的你应该使用的。

此外,您应该使用 SingletonEhCacheRegionFactory 来解决您的 junit 问题(单例意味着只有 1 个缓存管理器...因此没有 2 个缓存管理器具有与异常中所示相同的缓存名称)

net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory

请注意:对于 Hibernate 4,请使用 org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory 而不是 net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory

希望有帮助。

关于java - Hibernate 二级缓存和 JUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18032771/

相关文章:

java - JUnit 测试要求用户输入的方法

java - 为什么 java 代码没有显示正确的实例数量?

java - 更新 Java 循环中的总计

java - 用于查找整数的二进制和线性搜索程序。如果存在则回答 yes,如果不存在则回答 no。如何让二分查找工作?

java - HibernateTools 逆向工程工具没有为生成器添加 Annotation

java - LocalDateTime JPA - Java 8

java - 将 ImageView 的大小调整为另一个 ImageView 的大小的动画

mysql - Mysql中long varchar的最大限制是多少

java - Hibernate - 如何设置空值以便级联工作[有趣!]?

spring - Mockito 什么时候/然后不工作