Spring专题

GWT Spring + Hibernate +Infinispan数据网格

GWT Spring + Hibernate +Infinispan数据网格

最大化Hibernate性能是使用infinispan数据网格,使用Infinispan作为Hibernate的第二级缓存, Infinispan是JBoss Cache的继任者,Hiberante从版本3.5上支持Infinispan作为第二级缓存。

建议 Hibernate的配置,Hibernate和Infinispan使用JTA事务,以便在同一事务中完成一个操作。否则,数据库和二级缓存的操作将不被视为一个原子操作。

由于使用tomcat,没有提供JTA ,我们要实现在Spring的JTA环境的框架,使用Atomikos

配置persistence.xml :

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">

 <persistence-unit name="MyPersistenceUnit" transaction-type="JTA">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>

  <properties>
   <property name="hibernate.hbm2ddl.auto" value="update" />
   <property name="hibernate.show_sql" value="false" />
   <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />

   <property name="hibernate.transaction.manager_lookup_class" value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup" />

   <property name="hibernate.cache.use_second_level_cache" value="true"/>
   <property name="hibernate.cache.use_query_cache" value="true"/>
   <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.infinispan.InfinispanRegionFactory"/>
   <!--
   <property name="hibernate.cache.infinispan.entity.eviction.strategy" value= "LRU"/>
   <property name="hibernate.cache.infinispan.entity.eviction.wake_up_interval" value= "2000"/>
   <property name="hibernate.cache.infinispan.entity.eviction.max_entries" value= "5000"/>
   <property name="hibernate.cache.infinispan.entity.expiration.lifespan" value= "60000"/>
   <property name="hibernate.cache.infinispan.entity.expiration.max_idle" value= "30000"/>
   -->

  </properties>

 </persistence-unit>

</persistence>

配置Spring整合Atomikos JTA事务,配置applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:task="http://www.springframework.org/schema/task"
 xsi:schemaLocation="
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

 <context:component-scan base-package="com.javacodegeeks.gwtspring" />

 <task:annotation-driven executor="myExecutor"
 scheduler="myScheduler" />

 <task:executor id="myExecutor" pool-size="5" />

 <task:scheduler id="myScheduler" pool-size="10" />

 <tx:annotation-driven />

 <bean id="entityManagerFactory"
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="jpaVendorAdapter">
   <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
  </property>
 </bean>

 <bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
  init-method="init" destroy-method="close">
  <property name="uniqueResourceName" value="javacodegeeks" />
  <property name="xaDataSourceClassName"
   value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
  <property name="xaProperties">
   <props>
    <prop key="URL">jdbc:mysql://localhost:3306/javacodegeeks</prop>
    <prop key="user">***</prop>
    <prop key="password">***</prop>
   </props>
  </property>
  <property name="maxPoolSize" value="50" />
  <property name="minPoolSize" value="20" />
 </bean>

 <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
  init-method="init" destroy-method="close">
  <property name="forceShutdown" value="false" />
 </bean>

 <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.J2eeUserTransaction">
  <property name="transactionTimeout" value="300" />
 </bean>

 <bean id="transactionManager"
  class="org.springframework.transaction.jta.JtaTransactionManager"
  depends-on="atomikosTransactionManager,atomikosUserTransaction">
  <property name="transactionManager" ref="atomikosTransactionManager" />
  <property name="userTransaction" ref="atomikosUserTransaction" />
  <property name="allowCustomIsolationLevels" value="true" />
 </bean>

</beans>

案例实体代码:

… import statements here …

@Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL)
@Entity
@Table(name = "EMPLOYEE")
public class EmployeeDTO implements java.io.Serializable {
 
 private static final long serialVersionUID = 7440297955003302414L;

}

我们决定了缓存的并发策略为"TRANSACTIONAL",因为我们不仅要执行检索,也创建/更新/删除缓存对象的操作,当然这样性能会比较差。

 

 

 

猜你喜欢