<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 297,  comments - 1618,  trackbacks - 0
    原文出處:http://i.cn.yahoo.com/en.wan1982/blog/p_9/

    1. 在Hibernate配置文件中設(shè)置:
        

    <!-- Hibernate SessionFactory -->
        
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            
    <property name="dataSource" ref="dataSource"/>
            
    <property name="mappingResources">
            
    <list>
                
    <value>com/ouou/model/Videos.hbm.xml</value>   
             
    </list>
             
    </property>
            
    <property name="hibernateProperties">
                
    <props>
                    
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    
    <prop key="hibernate.current_session_context_class">thread</prop>
                    
    <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
                    
    <prop key="hibernate.query.substitutions">true 'Y'false 'N'</prop>
                    
    <!--add ehcache-->
                    
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                    
    <prop key="hibernate.cache.use_query_cache">false</prop><!-- 是否使用查詢(xún)緩存 -->
                    
    <!--
                    
    <prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
                    
    <prop key="hibernate.show_sql">true</prop>
                    
    -->
                    
    <!--<prop key="hibernate.transaction.auto_close_session">true</prop>-->
                    
    <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
                    
    <!-- Create/update the database tables automatically when the JVM starts up
                     
    <prop key="hibernate.hbm2ddl.auto">update</prop> -->
                    
    <!-- Turn batching off for better error messages under PostgreSQL -->
                    
    <prop key="hibernate.jdbc.batch_size">25</prop>
                    
    <!--
                    
    <prop key="hibernate.connection.pool_size">10</prop>
                    
    -->
                
    </props>
            
    </property>
        
    </bean>


        如果不設(shè)置“查詢(xún)緩存”,那么hibernate只會(huì)緩存使用load()方法獲得的單個(gè)持久化對(duì)象,如果想緩存使用findall()、 list()、Iterator()、createCriteria()、createQuery()等方法獲得的數(shù)據(jù)結(jié)果集的話,就需要設(shè)置hibernate.cache.use_query_cache true 才行

    2.首先設(shè)置EhCache,建立配置文件ehcache.xml,默認(rèn)的位置在class-path,可以放到你的src目錄下:

     <ehcache>

        
    <!-- Sets the path to the directory where cache .data files are created.

         If the path is a Java System Property it is replaced by
         its value in the running VM.
         The following properties are translated:
         user.home 
    - User's home directory
         user.dir - User's current working directory
         java.io.tmpdir - Default temp file path -->
         
    <!--<diskStore path="java.io.tmpdir"/>-->
         
    <diskStore path="/data/ehcache"/>

        
    <!--Default Cache configuration. These will applied to caches programmatically created through
            the CacheManager.

            The following attributes are required:

            maxElementsInMemory            
    - Sets the maximum number of objects that will be created in memory
            eternal                                     
    - Sets whether elements are eternal. If eternal,  timeouts are 
                                                                ignored and the element is never expired.
            overflowToDisk                      
    - Sets whether elements can overflow to disk when the in-memory cache
                                                            has reached the maxInMemory limit.

            The following attributes are optional:
            timeToIdleSeconds           
    - Sets the time to idle for an element before it expires.
                                                            i.e. The maximum amount of time between accesses before an
                                                            element expires Is only used 
    if the element is not eternal.
                                                            Optional attribute. A value of 
    0 means that an Element can idle
                                                           
    for infinity.The default value is 0.
            timeToLiveSeconds             
    - Sets the time to live for an element before it expires.
                                                             i.e. The maximum time between creation time and when an element 
                                                             expires.  Is only used 
    if the element is not eternal.
                                                             Optional attribute. A value of 
    0 means that and Element can live
                                                            
    for infinity.
                                                            The 
    default value is 0.
            diskPersistent                           
    - Whether the disk store persists between restarts of the Virtual
                                                                 Machine.
                                                             The 
    default value is false.
            diskExpiryThreadIntervalSeconds   
    - The number of seconds between runs of the disk expiry thread. 
                                                             The 
    default value  is 120 seconds.
            
    -->

        
    <defaultCache
            maxElementsInMemory
    ="10000"
            eternal
    ="false"
            overflowToDisk
    ="true"
            timeToIdleSeconds
    ="120"
            timeToLiveSeconds
    ="120"
            diskPersistent
    ="false"
            diskExpiryThreadIntervalSeconds
    ="120"/>
        
    <cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000" 
         eternal
    ="true" overflowToDisk="true"/>
        
    <cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="5" eternal="false"
        timeToLiveSeconds
    ="120" overflowToDisk="true"/>
        
    <cache name="userCache" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds=
            "
    600"    timeToLiveSeconds="600" overflowToDisk="false" diskPersistent="false"/>
        
    <cache name="com.ouou.webapp.util.OuouMethodIntecepter" maxElementsInMemory="100000" 
        eternal
    ="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false"
        diskPersistent
    ="false"/>
        
    <cache name="bbcode" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds="600"
        timeToLiveSeconds
    ="600" 
        overflowToDisk
    ="false" diskPersistent="false"/>
        
    <cache name="com.ouou.model.Videos" maxElementsInMemory="10000"  eternal="false" 
        overflowToDisk
    ="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
        
    <cache name="com.ouou.model.Tags" maxElementsInMemory="10000"  eternal="false"
        overflowToDisk
    ="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
    </ehcache>


    以com.ouou.model.Videos為例子
    在Videos.hbm.xml中配置:
    <class name="Videos" table="TEST" lazy="false">
      <cache usage="read-write" region="ehcache.xml中的name的屬性值"/>注意:這一句需要緊跟在class標(biāo)簽下面,其他位置無(wú)效。
    hbm文件查找cache方法名的策略:如果不指定hbm文件中的region="ehcache.xml中的name的屬性值",則使用name名為com.ouou.model.Videos的cache,
    如果不存在與類(lèi)名匹配的cache名稱(chēng),則用defaultCache。
    如果Videos包含set集合,則需要另行指定其cache
    例如Videos包含Tags集合,則需要
    添加如下配置到ehcache.xml中
    <cache name="com.ouou.model.Tags"
            maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"
            timeToLiveSeconds="120" overflowToDisk="false" />
    另,針對(duì)查詢(xún)緩存的配置如下:
    <cache name="org.hibernate.cache.UpdateTimestampsCache"
            maxElementsInMemory="5000"
            eternal="true"
            overflowToDisk="true"/>
    <cache name="org.hibernate.cache.StandardQueryCache"
            maxElementsInMemory="10000"
            eternal="false"
            timeToLiveSeconds="120"
            overflowToDisk="true"/>

    3、 選擇緩存策略依據(jù):

    <cache  usage="transactional|read-write|nonstrict-read-write|read-only" (1)/>
    ehcache不支持transactional,其他三種可以支持。
    read-only:無(wú)需修改, 那么就可以對(duì)其進(jìn)行只讀 緩存,注意,在此策略下,如果直接修改數(shù)據(jù)庫(kù),即使能夠看到前臺(tái)顯示效果,
    但是將對(duì)象修改至cache中會(huì)報(bào)error,cache不會(huì)發(fā)生作用。另:刪除記錄會(huì)報(bào)錯(cuò),因?yàn)椴荒茉趓ead-only模式的對(duì)象從cache中刪除。
    read-write:需要更新數(shù)據(jù),那么使用讀/寫(xiě)緩存 比較合適,前提:數(shù)據(jù)庫(kù)不可以為serializable transaction isolation level
    (序列化事務(wù)隔離級(jí)別)
    nonstrict-read-write:只偶爾需要更新數(shù)據(jù)(也就是說(shuō),兩個(gè)事務(wù)同時(shí)更新同一記錄的情況很不常見(jiàn)),也不需要十分嚴(yán)格的事務(wù)隔離,
    那么比較適合使用非嚴(yán)格讀/寫(xiě)緩存策略。

    4、 調(diào)試時(shí)候使用log4j的log4j.logger.org.hibernate.cache=debug,更方便看到ehcache的操作過(guò)程,主要用于調(diào)試過(guò)程,實(shí)際應(yīng)用發(fā)布時(shí)候,請(qǐng)注釋掉,以免影響性能。

    5、 使用ehcache,打印sql語(yǔ)句是正常的,因?yàn)閝uery cache設(shè)置為true將會(huì)創(chuàng)建兩個(gè)緩存區(qū)域:一個(gè)用于保存查詢(xún)結(jié)果集 (
    org.hibernate.cache.StandardQueryCache);另一個(gè)則用于保存最近查詢(xún)的一系列表的時(shí)間戳(org.hibernate.cache.UpdateTimestampsCache)。
    請(qǐng)注意:在查詢(xún)緩存中,它并不緩存結(jié)果集中所包含的實(shí)體的確切狀態(tài);它只緩存這些實(shí)體的標(biāo)識(shí)符屬性的值、以及各值類(lèi)型的結(jié)果。
    需要將打印sql語(yǔ)句與最近的cache內(nèi)容相比較,將不同之處修改到cache中,所以查詢(xún)緩存通常會(huì)和二級(jí)緩存一起使用。

    英文參考資料:http://ehcache.sourceforge.net/documentation/#mozTocId258426
    博文參考:http://blog.csdn.net/yun15291li/archive/2006/02/21/604095.aspx
                     http://zyl.javaeye.com/blog/68369
    其他:http://dev.yesky.com/157/2557157.shtml

    posted on 2008-06-30 21:45 阿蜜果 閱讀(7999) 評(píng)論(3)  編輯  收藏 所屬分類(lèi): Java


    FeedBack:
    # re: 【轉(zhuǎn)】配置ehcache.xml文件
    2009-11-26 13:29 | djb26
    “ 如果不設(shè)置“查詢(xún)緩存”,那么hibernate只會(huì)緩存使用load()方法獲得的單個(gè)持久化對(duì)象,"這是為什么?我不用查詢(xún)緩存時(shí)用get也能獲得緩存啊?  回復(fù)  更多評(píng)論
      
    # re: 【轉(zhuǎn)】配置ehcache.xml文件
    2010-12-26 15:33 | java戀人
    阿米果-朋友,你懂葡萄牙語(yǔ)?  回復(fù)  更多評(píng)論
      
    # re: 【轉(zhuǎn)】配置ehcache.xml文件
    2010-12-26 16:20 | 阿蜜果
    @java戀人
    沒(méi)有咯,只是喜歡Amigo這個(gè)英文名
      回復(fù)  更多評(píng)論
      
    <2008年6月>
    25262728293031
    1234567
    891011121314
    15161718192021
    22232425262728
    293012345

          生活將我們磨圓,是為了讓我們滾得更遠(yuǎn)——“圓”來(lái)如此。
          我的作品:
          玩轉(zhuǎn)Axure RP  (2015年12月出版)
          

          Power Designer系統(tǒng)分析與建模實(shí)戰(zhàn)  (2015年7月出版)
          
         Struts2+Hibernate3+Spring2   (2010年5月出版)
         

    留言簿(263)

    隨筆分類(lèi)

    隨筆檔案

    文章分類(lèi)

    相冊(cè)

    關(guān)注blog

    積分與排名

    • 積分 - 2294518
    • 排名 - 3

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 99在线观看视频免费| 国产无遮挡吃胸膜奶免费看| 国产乱辈通伦影片在线播放亚洲 | 国产一区二区三区亚洲综合| 国产午夜精品久久久久免费视| 国产麻豆一精品一AV一免费| 德国女人一级毛片免费| 亚洲av中文无码乱人伦在线播放| 亚洲福利视频一区二区三区| eeuss草民免费| 日韩免费视频播放| 亚洲成人午夜电影| 国产免费一区二区视频| 亚洲第一黄片大全| 中文字幕乱码亚洲精品一区| 无码免费一区二区三区免费播放 | 亚洲人成网站日本片| 91免费在线视频| 日韩午夜免费视频| 自拍日韩亚洲一区在线| 国产精品免费AV片在线观看| 日日噜噜噜噜夜夜爽亚洲精品| 亚洲精品美女在线观看| 叮咚影视在线观看免费完整版| 四虎在线视频免费观看视频| 亚洲av永久无码精品网站| 人成电影网在线观看免费| 精品久久洲久久久久护士免费| 国产亚洲大尺度无码无码专线| 亚洲日本国产精华液| 一区二区三区观看免费中文视频在线播放 | 永久免费无码网站在线观看| 亚洲成电影在线观看青青| 最近更新免费中文字幕大全| 亚洲成网777777国产精品| 亚洲欧美在线x视频| 日本无吗免费一二区| 亚洲国产精品无码第一区二区三区| GOGOGO免费观看国语| 怡红院亚洲怡红院首页| 日韩久久无码免费毛片软件|