posts - 431,  comments - 344,  trackbacks - 0
          你用正常方式定義你的 entity bean 類。JBoss EJB 3.0 將來的版本將支持 annotating entities 和所緩存的它們的關(guān)系的集合,但是現(xiàn)在你不得不直接配置底層的 hibernate 引擎。讓我們看看通過可選的property元素配置 hibernate 緩存選項(xiàng)的persistence.xml文件。下面persistence.xml 里的定義緩存的元素應(yīng)該被啟用:
          <!-- Clustered cache with TreeCache -->
          <property name="cache.provider_class">
          org.jboss.ejb3.entity.TreeCacheProviderHook
          </property>
           
          下面的屬性元素定義了所使用的緩存對(duì)象名和 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 {
          // ... ...
          }
           
              一個(gè)簡單的原則就是,你應(yīng)該對(duì)很少變動(dòng)和頻繁使用的對(duì)象進(jìn)行緩存.你可以在ejb3-entity-cache-service.xml配置文件里為每個(gè) entity bean 微調(diào)緩存設(shè)置。例如,你可以指定緩存的大小。如果緩存里的對(duì)象太多,緩存有可能擠掉最老的對(duì)象(或者最少用的對(duì)象,依你的配置而定)來給新對(duì)象留出空間。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),這個(gè)類的所有實(shí)例將象上面定義的一樣緩存在/_default區(qū)里。EJB3 Query API 提供了讓你在指定的緩存區(qū)里保存或載入查詢結(jié)果(就是 entity beans 的集合)的方法。
          posted on 2008-06-13 11:55 周銳 閱讀(625) 評(píng)論(0)  編輯  收藏 所屬分類: EJB 、Hibernate
          主站蜘蛛池模板: 永宁县| 汝州市| 泽州县| 隆子县| 砚山县| 龙海市| 西安市| 南宫市| 曲阜市| 洛宁县| 开阳县| 牟定县| 泸水县| 阆中市| 安多县| 济源市| 蕉岭县| 马鞍山市| 西平县| 定兴县| 沙雅县| 翁源县| 昌乐县| 唐海县| 新津县| 金寨县| 武功县| 桐庐县| 屏山县| 太原市| 隆回县| 栾城县| 长泰县| 增城市| 璧山县| 图木舒克市| 西城区| 九台市| 庆云县| 衡阳市| 石渠县|