小编典典

Apache-Ignite集成作为Hibernate 2级缓存无法启动?

hibernate

我正在尝试将Apache Ignite设置为项目中的第二级Hibernate缓存提供程序,但是却遇到了一个奇怪的异常。

配置如下:

  1. 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>

<repositories>
<repository>
    <id>GridGain External Repository</id>
    <url>http://www.gridgainsystems.com/nexus/content/repositories/external</url>
</repository>
</repositories>

    <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}</version>
    </dependency>
  1. Spring Hibernate配置文件

    <!-- Hibernate SessionFactory -->
    





    org.hibernate.dialect.MySQL5Dialect
    true
    true
    true
    true
    true
    true
    org.apache.ignite.cache.hibernate.HibernateRegionFactory
    hibernate-grid
    READ_ONLY


    1. ignite-configuration.xml
    <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”>



    <!-- Basic configuration for transactional cache. -->
    <bean id="transactional-cache"

class=”org.apache.ignite.configuration.CacheConfiguration” abstract=”true”>



    <bean id="ignite.cfg"

class=”org.apache.ignite.configuration.IgniteConfiguration”>

    <!--
        Specify cache configuration for each L2 cache region (which

corresponds
to a full class name or a full association name).
–>







            <!-- Configuration for query result cache. -->
            <bean parent="atomic-cache">
                <property name="name"

value=”org.hibernate.cache.internal.StandardQueryCache”/>


    </bean>

    </beans>
  1. 实体类
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;
  }

}
  1. IgniteAlphaCache管理器
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 ideaCache = new
IgniteAlphaCache<>(igniteCache);
caches.put(BaseIdea.class, ideaCache);
}

@Override
public AlphaCache<?, ?> getCache(Class<?> cacheClass) {
  return caches.get(cacheClass);
}

}

}

我在tomcat启动下面得到的例外:

Jul 27, 2016 1:46:43 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hibernateStatisticsFactoryBean': Unsatisfied dependency expressed through field 'sessionFactoryHibernate': Error creating bean with name 'sessionFactoryHibernate' defined in class path resource [spring/databaseContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Cache is not started: userType; nested exception is 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.IllegalArgumentException: Cache is not started: userType
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:350)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    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.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:756)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4811)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5272)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
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.IllegalArgumentException: Cache is not started: userType
    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.IllegalArgumentException: Cache is not started: userType
    at org.apache.ignite.internal.processors.cache.GridCacheProcessor.publicCache(GridCacheProcessor.java:3143)
    at org.apache.ignite.internal.IgniteKernal.getCache(IgniteKernal.java:2427)
    at org.apache.ignite.cache.hibernate.HibernateRegionFactory.regionCache(HibernateRegionFactory.java:226)
    at org.apache.ignite.cache.hibernate.HibernateRegionFactory.buildEntityRegion(HibernateRegionFactory.java:175)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:364)
    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

阅读 247

收藏
2020-06-20

共1个答案

小编典典

您必须为所有区域配置缓存。例如,userType您可以将其添加到配置中:

<bean parent="transactional-cache">
    <property name="name" value="userType"/>
</bean>
2020-06-20