JPA 2.0 配置

數據庫:SQLServer 2000   驅動:jtds-1.2.5.jar(http://sourceforge.net/projects/jtds/files/

爲什麼不用msbase.jar、mssqlserver.jar、msutil.jar?因爲這三個驅動文件有BUG。

JPA 2.0 :hibernate-jpa-2.0-api-1.0.0.Final.jar

二級緩存 :ehcache-core-2.4.2.jar(http://sourceforge.net/projects/ehcache/files/

① 使用JDBC 配置數據源

WEB-INF\classes\META-INF\persistence.xml

<persistence-unit name="JPATest" transaction-type="RESOURCE_LOCAL">
          <provider>org.hibernate.ejb.HibernatePersistence</provider>
          <shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>
          <properties>
                   <property name="javax.persistence.jdbc.driver" value="net.sourceforge.jtds.jdbc.Driver"/> 
                   <property name="javax.persistence.jdbc.url" value="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=demo"/> 
                   <property name="javax.persistence.jdbc.user" value="sa"/>
                   <property name="javax.persistence.jdbc.password" value="joe"/>
   
                   <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
                   <property name="hibernate.show_sql" value="true" />
                   <property name="hibernate.format_sql" value="false" />

                   <property name="hibernate.max_fetch_depth" value="3" /> 
                   <property name="hibernate.default_batch_fetch_size"  value="16"/>
                   <property name="hibernate.hbm2ddl.auto" value="none" />   
                   <property name="hibernate.jdbc.fetch_size" value="50" />   
                   <property name="hibernate.jdbc.batch_size" value="10" />
                   <!--啓用二級緩存-->
                   <property name="hibernate.cache.use_second_level_cache" value="true" />
            <!-- <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.EhCacheRegionFactory" /> -->

                   <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory" />
                   <property name="hibernate.cache.use_query_cache" value="true" />
                   <property name="net.sf.ehcache.configurationResourceName" value="ehcache.xml" />
           </properties>
 </persistence-unit>

② 使用Tomcat 的數據源

● WEB-INF\classes\META-INF\persistence.xml

<persistence-unit name="JPATest" transaction-type="RESOURCE_LOCAL">
           <provider>org.hibernate.ejb.HibernatePersistence</provider>
           <non-jta-data-source>java:comp/env/jdbc/ticket</non-jta-data-source>
           <shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>
           <properties>   
                   <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
                   <property name="hibernate.show_sql" value="true" />
                   <property name="hibernate.format_sql" value="false" />

                   <property name="hibernate.max_fetch_depth" value="3" /> 
                   <property name="hibernate.default_batch_fetch_size"  value="16"/>
                   <property name="hibernate.hbm2ddl.auto" value="none" />   
                   <property name="hibernate.jdbc.fetch_size" value="50" />   
                   <property name="hibernate.jdbc.batch_size" value="10" />
                   <!--啓用二級緩存-->
                   <property name="hibernate.cache.use_second_level_cache" value="true" />
                   <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory" />
                   <property name="hibernate.cache.use_query_cache" value="true" />
                   <property name="net.sf.ehcache.configurationResourceName" value="ehcache.xml" />
           </properties>
 </persistence-unit>

● tomcat\config\server.xml

<Resource name="jdbc/demoauth="Container
                              type="javax.sql.DataSource
                              driverClassName="net.sourceforge.jtds.jdbc.Driver
                              url="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=demo
                              username="sapassword="joemaxActive="20maxIdle="2maxWait="1000
                              removeAbandoned="trueremoveAbandonedTimeout="120logAbandoned="true"/>

● tomcat\config\context.xml

<ResourceLink name="jdbc/ticketglobal="jdbc/demo"   type="javax.sql.DataSource"/>


③ 使用JBOSS的數據源

 ● WEB-INF\classes\META-INF\persistence.xml

<persistence-unit name="JPATest" transaction-type="RESOURCE_LOCAL">
           <provider>org.hibernate.ejb.HibernatePersistence</provider>
           <non-jta-data-source>java:/etix_app</non-jta-data-source>
           <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
           <properties>   
                   <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
                   <property name="hibernate.show_sql" value="true" />
                   <property name="hibernate.format_sql" value="false" />

                   <property name="hibernate.max_fetch_depth" value="3" /> 
                   <property name="hibernate.default_batch_fetch_size"  value="16"/>
                   <property name="hibernate.hbm2ddl.auto" value="none" />   
                   <property name="hibernate.jdbc.fetch_size" value="50" />   
                   <property name="hibernate.jdbc.batch_size" value="10" />
                   <!--啓用二級緩存-->
                   <property name="hibernate.cache.use_second_level_cache" value="true" />
                   <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory" />
                   <property name="hibernate.cache.use_query_cache" value="true" />
                   <property name="net.sf.ehcache.configurationResourceName" value="ehcache.xml" />
           </properties>
 </persistence-unit>

● jboss-6.0.0.Final\server\default\deploy\mssql-ds.xml(在 jboss-6.0.0.Final\docs\examples\jca\ 目錄下可以找到 mssql-ds.xml 模板)

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
           <local-tx-datasource>
           <jndi-name>etix_app</jndi-name>
           <connection-url>jdbc:jtds:sqlserver://localhost:1433;DatabaseName=demo</connection-url>
           <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
           <user-name>sa</user-name>
           <password>joe</password>
           <metadata>
                       <type-mapping>MS SQLSERVER2000</type-mapping>
           </metadata>
           </local-tx-datasource>
</datasources>

④ 配置二級緩存

JPA2.0 配置 - 低調的華麗 - 輝色空間 
JPA2.0 配置 - 低調的華麗 - 輝色空間
JPA2.0 配置 - 低調的華麗 - 輝色空間
 

● WEB-INF\classes\ehcache.xml

<ehcache>   
            <diskStore path="java.io.tmpdir"/>   
            <!-- 設置緩存默認數據的過期策略 -->
            <defaultCache maxElementsInMemory="10000" 
                       eternal="false"  
                       timeToIdleSeconds="120" 
                       timeToLiveSeconds="120" 
                       overflowToDisk="true"/>   
            <!-- 設定具體的命名緩存的數據過期策略,每個命名緩存代表一個緩存區域,每個緩存區域有各自的數據過期策略。
                      命名緩存機制使得用戶可以在每個類以及每個類的集合的粒度上設置數據過期策略。
                      name: 緩存的名字,取值爲類的全限定名或類的集合的名字
                      maxElementsInMemory:設置基於內存的緩存中可存放的對象的最大數目
                      eternal: 設置對象是否爲永久,若爲true,將忽略timeToIdleSeconds和timeToLiveSeconds屬性,默認值(false)
                       timeToIdleSeconds: 設置對象空閒最長時間,超過時間就過期,EHCache 將從緩存中清除。如果爲0,表示對象可以無限地處於空閒狀態
                      timeToLiveSeconds:設置對象生存最長時間,超過時間就過期,EHCache 將從緩存中清除。如果爲0,表示對象可以無限地存在於緩存中
                      overflowToDisk:設置基於內存的緩存中的對象數目達到上限後,是否把溢出的對象寫到硬盤的緩存中
             -->
            <cache name="com.demo.test.beans.Student" 
                      maxElementsInMemory="10" 
                      eternal="false" 
                      timeToIdleSeconds="100" 
                      timeToLiveSeconds="100" 
                      overflowToDisk="false"/>

</ehcache>

● Static Configuration of the Cache

Caching can be configured at the level of the global persistence unit or on a per-class basis. It is achieved through a combination of a persistence unit setting and class settings.

The persistence unit cache setting is controlled by the shared-cache-mode element in the persistence.xml file.

It has five options:

① <shared-cache-mode>NOT_SPECIFIED</shared-cache-mode>
when the shared cache setting is not explicitly specified in the persistence.xml file or by the presence of the javax.persistence.sharedCache.mode property, it is up to the provider to either cache or not cache, depending upon its own defaults and inclinations.

② <shared-cache-mode>ALL</shared-cache-mode>
the shared cache to be completely enabled

③ <shared-cache-mode>NONE</shared-cache-mode>
the shared cache to be completely disabled

④ <shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>
the default behavior to cache every entity in the persistence unit.then annotating the specific entity class that is to remain uncached with @Cacheable(false)

for exsample:

@Entity
@Table(name="student")
@Cacheable(false)
public class Student implements Serializable {

⑤ <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
meaning that the default is to disable caching for all entities except those that have been annotated with @Cacheable(true) (or just @Cacheable because the default value of @Cacheable is true)

for example:@Entity
@Table(name="student")
@Cacheable
public class Student implements Serializable {

 

● JPA Cache

JPA2.0 配置 - 低調的華麗 - 輝色空間
JPA2.0 配置 - 低調的華麗 - 輝色空間
JPA2.0 配置 - 低調的華麗 - 輝色空間
JPA2.0 配置 - 低調的華麗 - 輝色空間
JPA2.0 配置 - 低調的華麗 - 輝色空間
JPA2.0 配置 - 低調的華麗 - 輝色空間
JPA2.0 配置 - 低調的華麗 - 輝色空間
JPA2.0 配置 - 低調的華麗 - 輝色空間
 
JPA2.0 配置 - 低調的華麗 - 輝色空間
JPA2.0 配置 - 低調的華麗 - 輝色空間
JPA2.0 配置 - 低調的華麗 - 輝色空間
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章