JPA2.0 配置
来源:互联网 发布:软件安全测试培训 编辑:程序博客网 时间:2024/05/16 06:04
为什么不用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
- JPA2.0 配置
- jta+spring4+hibernate5/jpa2配置
- JPA2.0的学习
- Spring3+Struts2+JPA2.0
- Spring3+Struts2+JPA2.0
- OpenJPA and JPA2.0
- Spring3+Struts2+JPA2.0
- JPA2.0 缓存
- JPA2.0总结
- JPA2.0查询测试
- 详解jpa2.0
- jpa2
- Maven Spring3.0+JPA2.0
- Sping3.0.3 and JPA2.0
- Spring3+Hibernate4+JPA2.0整合
- jpa2.0动态查询机制
- JPA2.0学习笔记---Criteria 查询
- javaEE6引入JPA2.0四大新特性
- WC on windows
- SAP HANA 7. SAP HANA load CSV 文件数据
- eclipse 版本挑选及常见使用问题
- 对象缓冲池--采用最近最久未使用策略(LRUCache )管理对象,同时带有事件监听功能
- Apache中如何使每个用户有独立的cgi-bin目录
- JPA2.0 配置
- 设计模式--适配器模式--UML
- Java 类中各成分加载顺序 和 内存中的存放位置
- 初识/dev/shm & oracle
- dos下批量卸载apk文件
- WCF——1.2 WCF服务、控制台宿主、控制台客户端
- INDEX
- 图片处理系列二Android瀑布流照片墙实现(含源码)
- 倒排索引