数据库: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/demo" auth="Container"
type="javax.sql.DataSource"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=demo"
username="sa" password="joe" maxActive="20" maxIdle="2" maxWait="1000"
removeAbandoned="true" removeAbandonedTimeout="120" logAbandoned="true"/>
● tomcat\config\context.xml
<ResourceLink name="jdbc/ticket" global="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>
④ 配置二级缓存
● 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>
for example:@Entity
@Table(name="student")
@Cacheable
public class Student implements Serializable {
● JPA Cache