设为首页 收藏本站
查看: 407|回复: 0

[经验分享] 转载:Spring + JPA + Hibernate + Tomcat + EHCache

[复制链接]

尚未签到

发表于 2017-1-30 11:44:27 | 显示全部楼层 |阅读模式
  Having @ManyToMany(fetch=FetchType.EAGER) attributes can slow down retrieval quite significantly (up to 40 times slower).
  I recall reading somewhere that FetchType.EAGER is the default for @ManyToMany associations. Also, from experience I noticed that setting FetchType.LAZY caused a org.hibernate.LazyInitializationException thrown with exception message similar to, failed to lazily initialize a collection of role: com.xyz.domain.EntityOne.images, no session or session was closed.
  So, it seemed that the only other way to quickly reduce the time it took (for the retrieval) was to look into the caching options such as query caching, second-level caching, both of which are supported by Hibernate (the webapp’s JPA provider). For more information on second-level caching, please refer to this article.
  Integrating Spring + JPA + Hibernate + Tomcat + EHCache took me a few hours this afternoon, but the effort paid off. The retrieval is now 40 times faster!
  This post is summarizing the setup involved in getting them all to work together.
  Relevant Section Of persistence.xml:

<provider>org.hibernate.ejb.HibernatePersistence</provider>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.cache.provider_class"
    value="org.hibernate.cache.SingletonEhCacheProvider" />
<property name="hibernate.cache.provider_configuration" value="/ehcache.xml" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
</properties>



  Notes:


  • The persistence.xml is located at {tomcat}/webapps/{your-webapp}/META-INF.
  • I used org.hibernate.cache.SingletonEhCacheProvider instead of org.hibernate.cache.EhCacheProvider. I was getting a WARN message if I used org.hibernate.cache.EhCacheProvider. I referred to this post for a fix.
  Relevant Section Of ehcache.xml:

  <diskStore path="user.dir/mywebapp-special-cache-folder"/>
 
<defaultCache eternal="false" overflowToDisk="false"
    maxElementsInMemory="1000" timeToIdleSeconds="30" timeToLiveSeconds="60"/>
 
<cache name="com.xyz.domain.EntityTwo" eternal="false" overflowToDisk="true"
    maxElementsInMemory="1000" timeToIdleSeconds="300" timeToLiveSeconds="600"
    diskPersistent="true" diskExpiryThreadIntervalSeconds="300"/>
 
<cache name="com.xyz.domain.EntityOne" eternal="false" overflowToDisk="true"
    maxElementsInMemory="1000" timeToIdleSeconds="300" timeToLiveSeconds="600"
    diskPersistent="true" diskExpiryThreadIntervalSeconds="300"/>
 
<cache name="com.xyz.domain.EntityOne.images" eternal="false"
    overflowToDisk="true"
    maxElementsInMemory="1000" timeToIdleSeconds="300" timeToLiveSeconds="600"
    diskPersistent="true" diskExpiryThreadIntervalSeconds="300"/>



  Notes:


  • The ehcache.xml is located at {tomcat}/webapps/{your-webapp}/WEB-INF/classes.
  • The diskStore element indicates where the files to be used for caching will be stored for the entities I wish to be made persistent to disk. In my web application, the files are stored under {tomcat}/bin/{mywebapp-special-cache-folder}

  • diskPersistent="true" indicates that the disk store (for the specific entity) is persistent between cache and VM restarts. Please refer to the EHCache documentation on disk storage for more information.
  Relevant JARs: under {tomcat}/webapps/{your-webapp}/WEB-INF/lib


  • ehcache-core-2.0.1.jar
  • hibernate3.jar
  • hibernate-jpa-2.0-api-1.0.0.Final.jar
  • slf4j-api-1.5.8.jar
  • slf4j-log4j12-1.5.6.jar
  • Spring 3.0.2 JARs..
  Relevant Section Of Spring Configuration:

<!-- there should be a way out of hardcoding the location of the properties file -->
<bean
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/spring-hes-db.properties" />
</bean>
 
<!-- ENTITY MANAGER FACTORY -->
<!-- LocalEntityManagerFactoryBean did not work for me -->
<bean id="emf-p"
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="pum"/>
<property name="persistenceUnitName" value="pu1"/>
<property name="dataSource" ref="dataSource-p" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
</bean>
</property>
</bean>
 
<!-- TRANSACTION MANAGER -->
<bean id="transactionManager"
  class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf-p"/>
<property name="dataSource" ref="dataSource-p"/>
</bean>
 
<!-- DATA SOURCES -->
<bean id="dataSource-p"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
    value="jdbc:mysql://${db.host}:${db.port}/${db.name}"/>
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
 
<!-- JPA TEMPLATE -->
<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory" ref="emf-p" />
</bean>
 
<!-- DAOs -->
<bean id="entityOneDao" class="hes.db.impl.EntityOneDAOImpl">
<property name="jpaTemplate" ref="jpaTemplate"/>
</bean>
 
<!-- PERSISTENCE UNIT -->
<bean id="pum"
  class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>META-INF/persistence.xml</value>
</list>
</property>
<property name="dataSources">
<map>
<entry key="remoteDataSource" value-ref="dataSource-p" />
</map>
</property>
<property name="defaultDataSource" ref="dataSource-p"/>
</bean>


转自:http://dev.nirmalya.net/articles/java/spring-jpa-hibernate-tomcat-ehcache

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-335290-1-1.html 上篇帖子: josso for tomcat配置 step by step 下篇帖子: Tomcat 源代码分析之Socket通讯
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表