0

我试图让 EHCache 在我的应用程序中工作。我做的第一件事是添加 Maven 依赖项:

pom.xml

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>${hibernate-version}</version>
    </dependency>

到目前为止一切顺利,现在在我的应用程序 root-context.xml 中(由于OpenSessionInView过滤器,SessionFactory 在 roout 中被拒绝)我为来自 jConsole 的 Hibernate 统计信息添加了 MBean,并完整定义了我的 sessionFactory:

根上下文.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="jmxExporter"
      class="org.springframework.jmx.export.MBeanExporter">
    <property name="beans">
        <map>
            <entry key="Hibernate:type=statistics">
                <ref local="statisticsBean"/>
            </entry>
        </map>
    </property>
</bean>
<bean id="statisticsBean" class="org.hibernate.jmx.StatisticsService">
    <property name="statisticsEnabled" value="true"/>
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="namingStrategy" class="com.execon.OracleNamingStrategy"/>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="oracle.jdbc.OracleDriver"/>
    <property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
    <property name="user" value="xxx"/>
    <property name="password" value="xxx"/>
    <property name="maxPoolSize" value="10"/>
    <property name="maxStatements" value="0"/>
    <property name="minPoolSize" value="5"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="namingStrategy" ref="namingStrategy"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    <property name="packagesToScan" value="com.execon.models"/>
</bean>
</beans>

是时候定义 hibernate.cfg.xml 和 ehcache 文件了,它们是:

休眠.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
    <property name="hibernate.cache.use_query_cache">true</property>
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</property>
    <property name="hibernate.generate_statistics">true</property>
</session-factory>
</hibernate-configuration>

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

<diskStore path="java.io.tmpdir"/>
<defaultCache
        eternal="false"
        maxElementsInMemory="1000"
        maxElementsOnDisk="10000"
        overflowToDisk="true"
        diskPersistent="true"
        timeToLiveSeconds="300"
        />
</ehcache>

一切都很好,所以现在是时候定义一些Service来测试缓存了,所以我做了:

@Service
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
@Transactional(readOnly = true)
public class MyService
{
    @Autowired
    private SessionFactory sessionFactory;

    @SuppressWarnings("unchecked")
    @Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
    public List<SettlementModelGroup> getModelGroups()
    {
        List<SettlementModelGroup> list = new ArrayList<SettlementModelGroup>();
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery( "from SettlementModelGroup" );

        list.addAll( query.list() );

        return list;
    }
}

如您所见,这个基本方法总是返回相同的列表。所以我正在检查休眠统计信息并且:

secondLevelCacheHitCount 0
secondLevelCacheMissCount 0
secondLevelCachePutCount 0

屏幕上的其余统计信息:

图表 链接太小:http ://s11.postimage.org/yfg9h6m83/image.jpg

那么怎么了,我错过了什么(很明显)?还是我走错了路?

编辑

SettlementModelGroup实体(也尝试过 CacheConcurrencyStrategy.READ_WRITE)

@Entity
@Table(name = "MODEL_GROUP")
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class SettlementModelGroup implements Serializable
{
@Id
@GeneratedValue(generator = "MODEL_GROUP_SEQ", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "MODEL_GROUP_SEQ", sequenceName = "SEQ_MODEL_GROUP_MODEL_GROUP_ID")
@Column(name = "MODEL_GROUP_ID", nullable = false)
private Integer modelId;

@Column(name = "NAME", nullable = false)
private String modelGroupName;

@Column(name = "DESCRIPTION", nullable = false)
private String modelGroupDescription;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "MODEL_GROUP_TYPE_ID", nullable = false)
private SettlementModelGroupType settlementModelGroupType;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PERIOD_TYPE_ID", nullable = false)
private PeriodType periodType;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "DOMAIN_ID")
private Domain domain;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "OWNER_ID", nullable = false)
private User user;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "modelId")
@Cascade(CascadeType.ALL)
private List<SettlementModel> settlementModels;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "STATUS_ID")
private Status status;

//getters and setters here
}
4

1 回答 1

4

穿上(您@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)SettlementModelGroup域实体)而不是服务方法。

另请参阅此链接。根据您的 EhCache 版本(2.4.3.?),您可能必须使用 CacheConcurrencyStrategy.READ_WRITE。

于 2012-07-26T02:26:49.373 回答