苏州区建设局网站首页,网站的成本,网站建设微信运营公司,展示网站方案gwt 同步和异步利用Infinispan Data Grid的功能最大化Hibernate性能。 一个GWT #xff0c; Spring #xff0c; JPA #xff0c; Hibernate #xff0c; Infinispan集成教程。 在本教程中#xff0c;我们将讨论如何将Infinispan用作Hibernate二级缓存提供程序。 Infinis… gwt 同步和异步 利用Infinispan Data Grid的功能最大化Hibernate性能。 一个GWT Spring JPA Hibernate Infinispan集成教程。 在本教程中我们将讨论如何将Infinispan用作Hibernate二级缓存提供程序。 Infinispan是JBoss缓存的继承者也是公司在开源数据网格领域的旗舰。 为了使事情变得更有趣我们将从上一篇关于Spring GWT Hibernate和JPA集成的文章的 结尾处继续。 我们将使用我们的GWTSpring项目 并使用数据网格功能对其进行授权 Hiberante从版本3.5开始就支持Infinispan作为二级缓存提供程序我们将使用Hibernate 3.5.2版本和Infinispan 4.0.0版本。 您可以在此处下载Infinispan二进制发行版 强烈建议将Hibernate配置为使用JTA事务以便Hibernate和Infinispan在同一事务中合作。 否则对数据库和二级缓存的操作将不会被视为单个工作单元。 这里的风险包括无法更新第二级缓存而当数据库正确提交数据时该二级缓存将保留陈旧数据。 由于我们将Web应用程序部署到一个独立的环境 Apache – Tomcat 中而不是为了完全满足上述要求而部署到一个完全的启用JTA的应用程序服务器中因此我们将在Spring中实现JTA环境。框架。 为此我们需要一个符合JTA的交易管理器而我们的首选客户是Atomikos 。 当然您可以使用您喜欢的任何符合JTA的事务管理器。 我们将使用Atomikos Transactions Essentials版本3.6.5您可以从此处下载 最后但并非最不重要的一点是我们将需要MySQL Connector / J连接到MySQL数据库进行测试。 Atomikos Transactions Essentials的3.6.5版本已经过测试可与MySQL Connector / J版本5.1.5配合使用可从此处下载 为了在运行时正确集成Infinispan和Hibernate 我们必须为Web应用程序提供所有必需的库。 因此复制下面在/ war / WEB-INF / lib下列出的文件如果使用的是不同版本请复制相关文件 从Infinispan二进制分发 infinispan-core.jar /lib/jboss-common-core-2.2.14.GA.jar /lib/jcip-annotations-1.0.jar /lib/jgroups-2.9.0.GA.jar /lib/marshalling-api-1.2.0.GA.jar /lib/rhq-pluginAnnotations-1.4.0.B01.jar /lib/river-1.2.0.GA.jar 从Atomikos Transactions Essentials发行 /dist/transactions-essentials-all.jar /lib/jca.jar /lib/jms.jar /lib/jmx.jar MySQL Connector / J mysql-connector-java-5.1.5-bin.jar 最后为了使Atomikos在运行时正常运行必须在我们的Web应用程序的类路径上找到一个“ jta.properties”文件。 创建一个名为“ jta.properties”的文本文件将其放在Eclipse项目的/ resources包下并使用以下所示的属性进行填充 com.atomikos.icatch.servicecom.atomikos.icatch.standalone.UserTransactionServiceFactory
com.atomikos.icatch.force_shutdown_on_vm_exit true
com.atomikos.icatch.automatic_resource_registration false
com.atomikos.icatch.console_log_level INFO 现在我们必须注意Eclipse项目的依赖性。 以下jars应该包含在项目的Java构建路径中 hibernate3.jar 下一步是将Hibernate配置为使用MySQL语言和二级缓存。 在/ resources / META-INF文件夹下找到persistence.xml文件然后执行以下描述的更改 要使用MySQL语言请添加以下属性 property namehibernate.dialect valueorg.hibernate.dialect.MySQL5InnoDBDialect / 要配置事务管理器以使用请添加以下属性 property namehibernate.transaction.manager_lookup_class valuecom.atomikos.icatch.jta.hibernate3.TransactionManagerLookup / 要启用启用了查询结果缓存的Hibernate二级缓存您应该添加以下属性 property namehibernate.cache.use_second_level_cache valuetrue/
property namehibernate.cache.use_query_cache valuetrue/ 在这一点上我们必须指出启用查询结果缓存可能不会提高性能特别是在您的应用程序执行主要返回唯一结果的查询的情况下。 要配置Infinispan缓存区域工厂请添加以下属性 property namehibernate.cache.region.factory_class valueorg.hibernate.cache.infinispan.InfinispanRegionFactory/ 我们还可以通过添加以下属性来配置驱逐策略此处为最近最少使用– LRU property namehibernate.cache.infinispan.entity.eviction.strategy value LRU/
property namehibernate.cache.infinispan.entity.eviction.wake_up_interval value 2000/
property namehibernate.cache.infinispan.entity.eviction.max_entries value 5000/
property namehibernate.cache.infinispan.entity.expiration.lifespan value 60000/
property namehibernate.cache.infinispan.entity.expiration.max_idle value 30000/ 通过使用逐出策略我们可以通过无条件地缓存对象来防止二级缓存占用所有可用的内存堆。 最近最少使用驱逐策略根据条目的使用频率逐出。 在我们的情况下将应用以下规则 “ wake_up_interval”属性定义控制器进程扫描二级缓存以查找候选逐出条目的频率此处为每2秒一次 “ max_entries”属性定义高速缓存条目的最大数量 “寿命”属性定义了对象可以保留在缓存中的最长时间。 如果达到了对象的使用寿命则无论访问频率如何此处为1分钟都将逐出该对象。 “ max_idle”属性定义在驱逐对象此处为30秒之前对象可以空闲不访问的最长时间。 最后我们必须将“持久性单元”“事务类型”属性更改为JTA 完整的persistence.xml文件应类似于以下提供的文件 persistence xmlnshttp://java.sun.com/xml/ns/persistence
xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocationhttp://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd
version2.0persistence-unit nameMyPersistenceUnit transaction-typeJTAproviderorg.hibernate.ejb.HibernatePersistence/providerpropertiesproperty namehibernate.hbm2ddl.auto valueupdate /property namehibernate.show_sql valuefalse /property namehibernate.dialect valueorg.hibernate.dialect.MySQL5InnoDBDialect /property namehibernate.transaction.manager_lookup_class valuecom.atomikos.icatch.jta.hibernate3.TransactionManagerLookup /property namehibernate.cache.use_second_level_cache valuetrue/property namehibernate.cache.use_query_cache valuetrue/property namehibernate.cache.region.factory_class valueorg.hibernate.cache.infinispan.InfinispanRegionFactory/!-- property namehibernate.cache.infinispan.entity.eviction.strategy value LRU/property namehibernate.cache.infinispan.entity.eviction.wake_up_interval value 2000/property namehibernate.cache.infinispan.entity.eviction.max_entries value 5000/property namehibernate.cache.infinispan.entity.expiration.lifespan value 60000/property namehibernate.cache.infinispan.entity.expiration.max_idle value 30000/--/properties/persistence-unit/persistence 下一步是配置有关JTA数据源 Atomikos事务管理器和JPA / Hibernate的 Spring 。 在/ war / WEB-INF /下找到您的applicationContext.xml文件并进行如下更改 beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:phttp://www.springframework.org/schema/pxmlns:aophttp://www.springframework.org/schema/aop xmlns:contexthttp://www.springframework.org/schema/contextxmlns:jeehttp://www.springframework.org/schema/jee xmlns:txhttp://www.springframework.org/schema/txxmlns:taskhttp://www.springframework.org/schema/taskxsi:schemaLocationhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsdcontext:component-scan base-packagecom.javacodegeeks.gwtspring /task:annotation-driven executormyExecutorschedulermyScheduler /task:executor idmyExecutor pool-size5 /task:scheduler idmyScheduler pool-size10 /tx:annotation-driven /bean identityManagerFactoryclassorg.springframework.orm.jpa.LocalContainerEntityManagerFactoryBeanproperty namedataSource refdataSource /property namejpaVendorAdapterbean classorg.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter //property/beanbean iddataSource classcom.atomikos.jdbc.AtomikosDataSourceBeaninit-methodinit destroy-methodcloseproperty nameuniqueResourceName valuejavacodegeeks /property namexaDataSourceClassNamevaluecom.mysql.jdbc.jdbc2.optional.MysqlXADataSource /property namexaPropertiespropsprop keyURLjdbc:mysql://localhost:3306/javacodegeeks/propprop keyuser***/propprop keypassword***/prop/props/propertyproperty namemaxPoolSize value50 /property nameminPoolSize value20 //beanbean idatomikosTransactionManager classcom.atomikos.icatch.jta.UserTransactionManagerinit-methodinit destroy-methodcloseproperty nameforceShutdown valuefalse //beanbean idatomikosUserTransaction classcom.atomikos.icatch.jta.J2eeUserTransactionproperty nametransactionTimeout value300 //beanbean idtransactionManagerclassorg.springframework.transaction.jta.JtaTransactionManagerdepends-onatomikosTransactionManager,atomikosUserTransactionproperty nametransactionManager refatomikosTransactionManager /property nameuserTransaction refatomikosUserTransaction /property nameallowCustomIsolationLevels valuetrue //bean/beans 这里要注意的事情 Spring Entity Manager Factory保留对数据源和JPA提供程序的引用以便为我们的DAO正确提供ORM功能 数据源被配置为XA资源。 如上所述这是强制性的以便Infinispan和数据库参与相同的事务。 您必须根据数据库配置更改“ xaProperties” URL用户和密码属性值。 我们将Spring配置为使用符合JTA的 Atomikos事务管理器。 我们快完成了 为了使实体可缓存我们仅需对其进行注释。 在/ shared / dto包下找到EmployeeDTO对象并添加Cache批注如下所示 … import statements here … Cache (usageCacheConcurrencyStrategy.TRANSACTIONAL)
Entity
Table(name EMPLOYEE)
public class EmployeeDTO implements java.io.Serializable {private static final long serialVersionUID 7440297955003302414L;…} 这里要注意的事情 我们将“高速缓存并发策略”指定为“ TRANSACTIONAL”因为我们不仅要对高速缓存的对象执行检索而且还要执行创建/更新/删除操作。 而已 要部署Web应用程序只需将/ war文件夹复制到Apache – Tomact“ webapps”文件夹中。 您可以将war文件夹的名称更改为任意名称最好在项目名称后重命名例如GWTSpringInfinispan 在午餐之前应用程序不要忘记创建数据库模式这里是“ javacodegeeks”。 午餐应用程序将您的浏览器指向以下地址 http// localhost8080 / GWTSpringInfinispan / 如果一切顺利您应该会看到您的主页。 应该显示两个文本框每个文本框后面都有一个按钮。 在第一个文本框中您可以将员工保存或更新到数据库。 作为输入提供ID名称姓氏和职位描述并用空格字符分隔。 单击“ SaveOrUpdate”按钮将提供的信息存储到数据库中。 对于现有员工条目相同的ID将执行更新。 第二个文本框用于检索现有员工条目。 提供员工ID然后单击“检索”按钮。 如果该员工存在则应该看到该员工的ID姓名姓氏和职位描述。 Atomikos事务管理器配置为在INFO级别生成日志记录请参见上面的“ jta.properties”文件。 日志文件位于Apache – Tomcat安装的bin目录中。 打开文件并观察对数据库执行的查询。 您应该期望以下内容 第一次对员工执行检索操作。 雇员对象未缓存并且对数据库执行查询。 现在应该缓存employee对象 对同一员工执行第二次检索操作。 现在不应该执行任何查询。 从Infinispan检索数据 对同一员工执行更新操作。 在单个工作单元中对Infinispan和数据库都执行更新操作 对同一员工执行第三次查询。 不应对数据库执行任何查询您应该看到上一步中更新的数据 您可以从此处下载该项目如开头所述并且不包含先前的文章所需的第三方库 玩得开心 贾斯汀 相关文章 GWT 2 Spring 3 JPA 2 Hibernate 3.5教程 Spring 3 HornetQ 2.1集成教程 Spring 3 RESTful Web服务 GWT 2 Spring 3 JPA 2 Hibernate 3.5教程– Eclipse和Maven 2展示 具有Spring和Maven教程的JAX–WS 翻译自: https://www.javacodegeeks.com/2010/06/gwt-spring-and-hibernate-enter-world-of.htmlgwt 同步和异步