聽聽聽聽
<!--聽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><!--聽鏄惁浣跨敤鏌ヨ緙撳瓨聽-->
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<!--
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<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>
聽聽聽 濡傛灉涓嶈緗滄煡璇㈢紦瀛樷濓紝閭d箞hibernate鍙細緙撳瓨浣跨敤load()鏂規硶鑾峰緱鐨勫崟涓寔涔呭寲瀵硅薄錛屽鏋滄兂緙撳瓨浣跨敤findall()銆?list()銆両terator()銆乧reateCriteria()銆乧reateQuery()絳夋柟娉曡幏寰楃殑鏁版嵁緇撴灉闆嗙殑璇濓紝灝遍渶瑕佽緗甴ibernate.cache.use_query_cache true 鎵嶈
2.棣栧厛璁劇疆EhCache錛屽緩绔嬮厤緗枃浠秂hcache.xml錛岄粯璁ょ殑浣嶇疆鍦╟lass-path錛屽彲浠ユ斁鍒頒綘鐨剆rc鐩綍涓嬶細
聽<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>
浠om.ouou.model.Videos涓轟緥瀛?br />鍦╒ideos.hbm.xml涓厤緗細
<class name="Videos" table="TEST" lazy="false">
聽 <cache usage="read-write" region="ehcache.xml涓殑name鐨勫睘鎬у?/>娉ㄦ剰錛氳繖涓鍙ラ渶瑕佺揣璺熷湪class鏍囩涓嬮潰錛屽叾浠栦綅緗棤鏁堛?br />hbm鏂囦歡鏌ユ壘cache鏂規硶鍚嶇殑絳栫暐錛氬鏋滀笉鎸囧畾hbm鏂囦歡涓殑region="ehcache.xml涓殑name鐨勫睘鎬у?錛屽垯浣跨敤name鍚嶄負com.ouou.model.Videos鐨刢ache錛?br />濡傛灉涓嶅瓨鍦ㄤ笌綾誨悕鍖歸厤鐨刢ache鍚嶇О錛屽垯鐢╠efaultCache銆?br />濡傛灉Videos鍖呭惈set闆嗗悎錛屽垯闇瑕佸彟琛屾寚瀹氬叾cache
渚嬪Videos鍖呭惈Tags闆嗗悎錛屽垯闇瑕?br />娣誨姞濡備笅閰嶇疆鍒癳hcache.xml涓?br /><cache name="com.ouou.model.Tags"
聽聽聽聽聽聽聽 maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"
聽聽聽聽聽聽聽 timeToLiveSeconds="120" overflowToDisk="false" />
鍙︼紝閽堝鏌ヨ緙撳瓨鐨勯厤緗涓嬶細
<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銆?閫夋嫨緙撳瓨絳栫暐渚濇嵁錛?br />
<cache聽 usage="transactional|read-write|nonstrict-read-write|read-only" (1)/>
ehcache涓嶆敮鎸乼ransactional錛屽叾浠栦笁縐嶅彲浠ユ敮鎸併?br />read-only錛氭棤闇淇敼錛?閭d箞灝卞彲浠ュ鍏惰繘琛屽彧璇?緙撳瓨錛屾敞鎰忥紝鍦ㄦ絳栫暐涓嬶紝濡傛灉鐩存帴淇敼鏁版嵁搴擄紝鍗充嬌鑳藉鐪嬪埌鍓嶅彴鏄劇ず鏁堟灉錛?br />浣嗘槸灝嗗璞′慨鏀硅嚦cache涓細鎶rror錛宑ache涓嶄細鍙戠敓浣滅敤銆傚彟錛氬垹闄よ褰曚細鎶ラ敊錛屽洜涓轟笉鑳藉湪read-only妯″紡鐨勫璞′粠cache涓垹闄ゃ?br />read-write錛氶渶瑕佹洿鏂版暟鎹紝閭d箞浣跨敤璇?鍐欑紦瀛?姣旇緝鍚堥傦紝鍓嶆彁錛氭暟鎹簱涓嶅彲浠ヤ負serializable transaction isolation level
錛堝簭鍒楀寲浜嬪姟闅旂綰у埆錛?br />nonstrict-read-write錛氬彧鍋跺皵闇瑕佹洿鏂版暟鎹紙涔熷氨鏄錛屼袱涓簨鍔″悓鏃舵洿鏂板悓涓璁板綍鐨勬儏鍐靛緢涓嶅父瑙侊級錛屼篃涓嶉渶瑕佸崄鍒嗕弗鏍肩殑浜嬪姟闅旂錛?br />閭d箞姣旇緝閫傚悎浣跨敤闈炰弗鏍艱/鍐欑紦瀛樼瓥鐣ャ?br />
4銆?璋冭瘯鏃跺欎嬌鐢╨og4j鐨刲og4j.logger.org.hibernate.cache=debug錛屾洿鏂逛究鐪嬪埌ehcache鐨勬搷浣滆繃紼嬶紝涓昏鐢ㄤ簬璋冭瘯榪囩▼,瀹為檯搴旂敤鍙戝竷鏃跺欙紝璇鋒敞閲婃帀錛屼互鍏嶅獎鍝嶆ц兘銆?br />
5銆?浣跨敤ehcache錛屾墦鍗皊ql璇彞鏄甯哥殑錛屽洜涓簈uery cache璁劇疆涓簍rue灝嗕細鍒涘緩涓や釜緙撳瓨鍖哄煙錛氫竴涓敤浜庝繚瀛樻煡璇㈢粨鏋滈泦 (
org.hibernate.cache.StandardQueryCache)錛涘彟涓涓垯鐢ㄤ簬淇濆瓨鏈榪戞煡璇㈢殑涓緋誨垪琛ㄧ殑鏃墮棿鎴?org.hibernate.cache.UpdateTimestampsCache)銆?br />璇鋒敞鎰忥細鍦ㄦ煡璇㈢紦瀛樹腑錛屽畠騫朵笉緙撳瓨緇撴灉闆嗕腑鎵鍖呭惈鐨勫疄浣撶殑紜垏鐘舵侊紱瀹冨彧緙撳瓨榪欎簺瀹炰綋鐨勬爣璇嗙灞炴х殑鍊箋佷互鍙婂悇鍊肩被鍨嬬殑緇撴灉銆?br />闇瑕佸皢鎵撳嵃sql璇彞涓庢渶榪戠殑cache鍐呭鐩告瘮杈冿紝灝嗕笉鍚屼箣澶勪慨鏀瑰埌cache涓紝鎵浠ユ煡璇㈢紦瀛橀氬父浼氬拰浜岀駭緙撳瓨涓璧蜂嬌鐢ㄣ?/p>
]]>