1
我想在我的項目中將Apache Ignite設置爲第二級Hibernate緩存提供程序,但我遇到了一個奇怪的異常。 配置是如下:Apache-Ignite整合爲Hibernate二級緩存中斷?
- 的pom.xml
新增
<spring.version>4.3.0.RELEASE</spring.version>
<hibernate.version>4.3.8.Final</hibernate.version>
<ignite.version>1.6.0</ignite.version>
<ignite.version.hibernate>1.0.0</ignite.version.hibernate>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-core</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-hibernate</artifactId>
<version>${ignite.version.hibernate}</version>
</dependency>
彈簧Hibernate配置文件
<!-- Hibernate SessionFactory --> <bean id="sessionFactoryHibernate" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.hbm2ddl.auto">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.generate_statistics">true</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.region.factory_class">org.apache.ignite.cache.hibernate.HibernateRegionFactory</prop> <prop key="org.apache.ignite.hibernate.grid_name">hibernate-grid</prop> <prop key="org.apache.ignite.hibernate.default_access_type">READ_ONLY</prop> </props> </property> <property name="packagesToScan" value="com.entity"></property>
- 點燃-configuration.xml文件
- 實體類
- IgniteAlphaCachemanager
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Basic configuration for atomic cache. --> <bean id="atomic-cache" class="org.apache.ignite.configuration.CacheConfiguration" abstract="true"> <property name="cacheMode" value="PARTITIONED"/> <property name="atomicityMode" value="ATOMIC"/> <property name="writeSynchronizationMode" value="FULL_SYNC"/> </bean> <!-- Basic configuration for transactional cache. --> <bean id="transactional-cache" class="org.apache.ignite.configuration.CacheConfiguration" abstract="true"> <property name="cacheMode" value="PARTITIONED"/> <property name="atomicityMode" value="TRANSACTIONAL"/> <property name="writeSynchronizationMode" value="FULL_SYNC"/> </bean> <bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration"> <!-- Specify the name of the caching grid (should correspond to the one in Hibernate configuration). --> <property name="gridName" value="hibernate-grid"/> <!-- Specify cache configuration for each L2 cache region (which corresponds to a full class name or a full association name). --> <property name="cacheConfiguration"> <list> <!-- Configurations for entity caches. --> <!-- <bean parent="transactional-cache"> <property name="name" value="com.mycompany.MyEntity1"/> </bean> <bean parent="transactional-cache"> <property name="name" value="com.mycompany.MyEntity2"/> </bean> <bean parent="transactional-cache"> <property name="name" value="com.mycompany.MyEntity1.children"/> </bean> --> <!-- Configuration for update timestamps cache. --> <bean parent="atomic-cache"> <property name="name" value="org.hibernate.cache.spi.UpdateTimestampsCache"/> </bean> <!-- Configuration for query result cache. --> <bean parent="atomic-cache"> <property name="name" value="org.hibernate.cache.internal.StandardQueryCache"/> </bean> </list> </property> </bean> </beans>
import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Index; import javax.persistence.Table; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; @Entity @Table(name = "USER_TYPE", indexes = { @Index(columnList = "TYPE_SHORT_NAME", name = "TYPE_SHORT_NAME_UNIQUE_idx", unique = true), }) @Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region = "userType") public class UserType implements Serializable { private static final long serialVersionUID = -628308304752474026L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "USER_TYPE_ID") private int userTypeId; @Column(name = "TYPE_SHORT_NAME", length = 20, nullable = false) private String typeShortName; @Column(name = "TYPE_LONG_NAME", length = 255) private String typeLongName; public UserType() { } public UserType(int userTypeId, String typeShortName, String typeLongName) { this.userTypeId = userTypeId; this.typeShortName = typeShortName; this.typeLongName = typeLongName; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((typeLongName == null) ? 0 : typeLongName.hashCode()); result = prime * result + ((typeShortName == null) ? 0 : typeShortName.hashCode()); result = prime * result + userTypeId; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof UserType)) return false; UserType other = (UserType) obj; if (typeLongName == null) { if (other.typeLongName != null) return false; } else if (!typeLongName.equals(other.typeLongName)) return false; if (typeShortName == null) { if (other.typeShortName != null) return false; } else if (!typeShortName.equals(other.typeShortName)) return false; if (userTypeId != other.userTypeId) return false; return true; } @Override public String toString() { return "UserType [userTypeId=" + userTypeId + ", typeShortName=" + typeShortName + ", typeLongName=" + typeLongName + "]"; } public int getUserTypeId() { return userTypeId; } public void setUserTypeId(int userTypeId) { this.userTypeId = userTypeId; } public String getTypeShortName() { return typeShortName; } public void setTypeShortName(String typeShortName) { this.typeShortName = typeShortName; } public String getTypeLongName() { return typeLongName; } public void setTypeLongName(String typeLongName) { this.typeLongName = typeLongName; } }
public interface AlphaCacheManager { AlphaCache<?, ?> getCache(Class<?> cacheClass); } import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.apache.ignite.Ignite; import org.apache.ignite.IgniteCache; import org.apache.ignite.Ignition; import org.springframework.stereotype.Repository; @Repository public class IgniteAlphaCacheManager implements AlphaCacheManager { private final Ignite ignite; private final Map<Class<?>, AlphaCache<?, ?>> caches; public IgniteAlphaCacheManager() { System.out.println("Init cache..."); ignite = Ignition.start("classpath:/spring/ignite-configuration.xml"); //ignite = Ignition.start(); caches = new ConcurrentHashMap<>(); initCaches(); } private void initCaches() { IgniteCache<Integer, BaseIdea> igniteCache = ignite.getOrCreateCache(BaseIdea.class.getName()); AlphaCache<Integer, BaseIdea> ideaCache = new IgniteAlphaCache<>(igniteCache); caches.put(BaseIdea.class, ideaCache); } @Override public AlphaCache<?, ?> getCache(Class<?> cacheClass) { return caches.get(cacheClass); } } }
,我得到它下面上Tomcat啓動異常:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactoryHibernate' defined in class path resource [spring/databaseContext.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/ignite/internal/processors/cache/CacheProjection
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:187)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1048)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:570)
... 24 more
Caused by: java.lang.NoClassDefFoundError: org/apache/ignite/internal/processors/cache/CacheProjection
at org.apache.ignite.cache.hibernate.HibernateRegionFactory.buildTimestampsRegion(HibernateRegionFactory.java:189)
at org.hibernate.cache.spi.UpdateTimestampsCache.<init>(UpdateTimestampsCache.java:73)
at org.hibernate.internal.CacheImpl.<init>(CacheImpl.java:72)
at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:40)
at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:35)
at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:91)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:251)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:225)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:273)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 35 more
Caused by: java.lang.ClassNotFoundException: org.apache.ignite.internal.processors.cache.CacheProjection
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139)
... 52 more
感謝解決我的例外,但我遇到了下面現在描述的另一個問題。 –