posts - 431,  comments - 344,  trackbacks - 0
          你用正常方式定義你的 entity bean 類。JBoss EJB 3.0 將來的版本將支持 annotating entities 和所緩存的它們的關系的集合,但是現在你不得不直接配置底層的 hibernate 引擎。讓我們看看通過可選的property元素配置 hibernate 緩存選項的persistence.xml文件。下面persistence.xml 里的定義緩存的元素應該被啟用:
          <!-- 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 被緩存的內容。就像上面所展示的樣,缺省是什么都不緩存。我們使用@Cache 注解來標記需要緩存的 entity beans。
          @Entity
          @Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
          public class Customer implements Serializable {
          // ... ...
          }
           
              一個簡單的原則就是,你應該對很少變動和頻繁使用的對象進行緩存.你可以在ejb3-entity-cache-service.xml配置文件里為每個 entity bean 微調緩存設置。例如,你可以指定緩存的大小。如果緩存里的對象太多,緩存有可能擠掉最老的對象(或者最少用的對象,依你的配置而定)來給新對象留出空間。mycompany.Customer entity bean 的緩存區(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 類指定緩存區(cache region),這個類的所有實例將象上面定義的一樣緩存在/_default區里。EJB3 Query API 提供了讓你在指定的緩存區里保存或載入查詢結果(就是 entity beans 的集合)的方法。
          posted on 2008-06-13 11:55 周銳 閱讀(625) 評論(0)  編輯  收藏 所屬分類: EJBHibernate
          主站蜘蛛池模板: 福安市| 台东县| 伊吾县| 富平县| 藁城市| 道真| 浪卡子县| 万年县| 久治县| 恭城| 宁安市| 无棣县| 道孚县| 景东| 海安县| 南安市| 万宁市| 延川县| 华阴市| 隆德县| 车致| 河间市| 台中市| 白河县| 玉田县| 青海省| 勐海县| 宜丰县| 玛曲县| 南昌市| 蒲城县| 慈溪市| 勐海县| 汾阳市| 嘉鱼县| 台安县| 灵山县| 曲周县| 萝北县| 奉新县| 克东县|