你用正常方式定義你的 entity bean 類。JBoss EJB 3.0 將來的版本將支持 annotating entities 和所緩存的它們的關(guān)系的集合,但是現(xiàn)在你不得不直接配置底層的 hibernate 引擎。讓我們看看通過可選的property元素配置 hibernate 緩存選項的persistence.xml文件。下面persistence.xml 里的定義緩存的元素應(yīng)該被啟用:
<!-- Clustered cache with TreeCache -->
<property name="cache.provider_class">
org.jboss.ejb3.entity.TreeCacheProviderHook
</property>
下面的屬性元素定義了所使用的緩存對象名和 MBean 名。
<property name="treecache.mbean.object_name">
jboss.cache:service=EJB3EntityTreeCache
</property>
下一步我們需要配置 entities 被緩存的內(nèi)容。就像上面所展示的樣,缺省是什么都不緩存。我們使用@Cache 注解來標(biāo)記需要緩存的 entity beans。
@Entity
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
public class Customer implements Serializable {
// ... ...
}
一個簡單的原則就是,你應(yīng)該對很少變動和頻繁使用的對象進(jìn)行緩存.你可以在ejb3-entity-cache-service.xml配置文件里為每個 entity bean 微調(diào)緩存設(shè)置。例如,你可以指定緩存的大小。如果緩存里的對象太多,緩存有可能擠掉最老的對象(或者最少用的對象,依你的配置而定)來給新對象留出空間。mycompany.Customer entity bean 的緩存區(qū)(cache region)是/mycompany/Customer。
<server>
<mbean code="org.jboss.cache.TreeCache"
name="jboss.cache:service=EJB3EntityTreeCache">
<depends>jboss:service=Naming
<depends>jboss:service=TransactionManager
... ...
<attribute name="EvictionPolicyConfig">
<config>
<attribute name="wakeUpIntervalSeconds">5</attribute>
<region name="/_default_">
<attribute name="maxNodes">5000</attribute>
<attribute name="timeToLiveSeconds">1000</attribute>
</region>
<region name="/mycompany/Customer">
<attribute name="maxNodes">10</attribute>
<attribute name="timeToLiveSeconds">5000</attribute>
</region>
... ...
</config>
</attribute>
</mbean>
</server>
如果你沒有為 entity bean 類指定緩存區(qū)(cache region),這個類的所有實例將象上面定義的一樣緩存在/_default區(qū)里。EJB3 Query API 提供了讓你在指定的緩存區(qū)里保存或載入查詢結(jié)果(就是 entity beans 的集合)的方法。
posted on 2008-06-13 11:55
周銳 閱讀(621)
評論(0) 編輯 收藏 所屬分類:
EJB 、
Hibernate