我使用ehcache时出错

时间:2017-03-01 10:14:44

标签: java spring hibernate spring-boot ehcache

我是新用的ehcache,我使用下面的设置。当我设置ehcache 3时,我的项目编译良好,但是当我在端点上发送查询时,我收到错误:

2017-03-01 11:46:02,032 ERROR org.springframework.transaction.interceptor.TransactionInterceptor : Application exception overridden by rollback error
java.lang.AbstractMethodError: org.hibernate.cache.ehcache.internal.regions.EhcacheTimestampsRegion.put(Lorg/hibernate/engine/spi/SessionImplementor;Ljava/lang/Object;Ljava/lang/Object;)V
    at org.hibernate.cache.spi.UpdateTimestampsCache.preInvalidate(UpdateTimestampsCache.java:95)
    at org.hibernate.engine.spi.ActionQueue.invalidateSpaces(ActionQueue.java:618)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:584)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1295)

application.gradle

   buildscript {
    ext {
        springBootVersion = '1.3.5.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'spring-boot'
apply plugin: 'war'


war {
    baseName = 'deliverypoint'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    providedRuntime
}

ext {
    jodaTimeVersion = '2.9.4'
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-jersey')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.apache.tomcat.embed:tomcat-embed-jasper')
    compile('org.postgresql:postgresql')
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile("joda-time:joda-time:$jodaTimeVersion")
    compile("org.apache.tomcat:tomcat-dbcp:7.0.55")
    compile group: 'org.jadira.usertype', name: 'usertype.jodatime', version: '2.0.1'
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.1.0.Final'
    compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.1.0.Final'
    compile group: 'org.hibernate', name: 'hibernate-validator', version: '5.2.4.Final'
    compile(group: 'org.apache.tiles', name: 'tiles-jsp', version: '3.0.5')

    compile('org.springframework.security:spring-security-taglibs')

    providedRuntime('javax.servlet:jstl')


    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.springframework.security.oauth:spring-security-oauth2")
    compile group: 'org.springframework.social', name: 'spring-social-core', version: '1.1.4.RELEASE'
    compile group: 'org.springframework.social', name: 'spring-social-security', version: '1.1.4.RELEASE'

    compile group: 'org.modelmapper.extensions', name: 'modelmapper-spring', version: '0.7.7'

    // https://mvnrepository.com/artifact/com.google.maps/google-maps-services
    compile group: 'com.google.maps', name: 'google-maps-services', version: '0.1.16'

//    compile (group: 'org.hibernate', name: 'hibernate-ehcache', version: '5.2.1.Final' ) {
//        exclude group: 'net.sf.ehcache', module: 'ehcache' //by both name and group
//    }
//
//    compile group: 'org.ehcache', name: 'ehcache', version: '3.3.0'


    compile group: 'net.sf.ehcache', name: 'ehcache-core', version: '2.6.11'
    compile group: 'org.hibernate', name: 'hibernate-ehcache', version: '5.2.1.Final'
    compile group: 'org.springframework', name: 'spring-context-support', version: '4.1.4.RELEASE'
    //    compile('org.springframework.boot:spring-boot-starter-cache')
}

bootRun {
    systemProperties System.properties
}

配置休眠:

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AvailableSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

import javax.sql.DataSource;
import java.util.Map;
import java.util.Properties;
@Configuration
    public class DatabaseConfig {

        @Autowired
        private Environment env;

        @Autowired
        private ApplicationContext applicationContext;

        @Bean
        public Map<String, DataSource> dataSourceMap() {
            return applicationContext.getBeansOfType(DataSource.class);
        }

        /**
         * init startup resources like sql scripts
         *
         * @return DataSourceInitializer
         */

        @Bean
        public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
            HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager(sessionFactory);
            hibernateTransactionManager.setAutodetectDataSource(false);
            return hibernateTransactionManager;
        }

        @Bean
        public HibernateTemplate hibernateTemplateMain(SessionFactory sessionFactory) {
            return new HibernateTemplate(sessionFactory);
        }

        public Properties hibernateProperties() {
            return new Properties() {
                {
                    setProperty("hibernate.hbm2ddl.auto", env.getProperty("datasources.ddl-auto"));
                    setProperty("hibernate.dialect", env.getProperty("datasources.hibernate.dialect"));
                    setProperty("hibernate.show_sql", env.getProperty("datasources.show-sql"));
                    setProperty("hibernate.format_sql", env.getProperty("datasources.format-sql"));
                    setProperty("hibernate.multiTenancy", env.getProperty("datasources.multiTenancyStrategy"));
                    setProperty("hibernate.multi_tenant_connection_provider", env.getProperty("datasources.multiTenancyConnectionProvider"));
                    setProperty("hibernate.tenant_identifier_resolver", env.getProperty("datasources.multiTenancyIdentifierResolver"));
                    setProperty("hibernate.default_schema", env.getProperty("datasources.default_schema"));
                    setProperty(AvailableSettings.USE_GET_GENERATED_KEYS, "true");

                    setProperty(AvailableSettings.USE_SECOND_LEVEL_CACHE, "true");
                    setProperty(AvailableSettings.USE_QUERY_CACHE, "true");
                    setProperty(AvailableSettings.CACHE_REGION_FACTORY, "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
                    setProperty("net.sf.ehcache.configurationResourceName","/ehcache.xml");
                }
            };
        }

        @Bean
        public LocalSessionFactoryBean sessionFactoryMain(Map<String, DataSource> dataSourceMap) {
            DataSource dataSource = dataSourceMap.get("datasourcemain");
            LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
            sessionFactory.setDataSource(dataSource);
            sessionFactory.setPackagesToScan("io.default.deliverypoints");
            sessionFactory.setHibernateProperties(hibernateProperties());
            return sessionFactory;
        }        
    } 

ehcache.xml中

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
         monitoring="autodetect" dynamicConfig="true" shared="true">

    <diskStore path="java.io.tmpdir/ehcache" />

    <defaultCache maxEntriesLocalHeap="5000" eternal="true"
                  timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30"
                  maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
                  memoryStoreEvictionPolicy="LRU" statistics="true">
        <persistence strategy="localTempSwap" />
    </defaultCache>

    <cache name="billCache" maxEntriesLocalHeap="10000"
           eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0">
        <persistence strategy="localTempSwap" />
    </cache>

    <cache name="org.hibernate.cache.internal.StandardQueryCache"
           maxEntriesLocalHeap="5000" eternal="false" timeToLiveSeconds="120">
        <persistence strategy="localTempSwap" />
    </cache>

    <cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
           maxEntriesLocalHeap="5000" eternal="true">
        <persistence strategy="localTempSwap" />
    </cache>
</ehcache> 

在主春季启动应用程序中,我使用@EnableCaching。 和注释,我在实体上使用:

@SuppressWarnings("serial")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="billCache")
@Cacheable

2 个答案:

答案 0 :(得分:0)

您将获得一个抽象方法异常,因为您需要在hibernate和ehcache之间连接提供程序。

执行此操作的方法位于documentation here

简而言之,hibernate属性的配置部分也需要包括:

         setProperty("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");

答案 1 :(得分:0)

对我来说,错误是我忘了在jboss-deployment-structure.xml中排除JBoss的集成org.hibernate模块,同时在项目的pom.xml中指定更新版本的hibernate-core。因此,该项目混合了ehcache的API。

src \ main \ webapp \ WEB-INF \ jboss-deployment-structure.xml必须如下所示:

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.1">
<deployment>
    <exclusions>
        <module name="org.hibernate"/>
    </exclusions>
</deployment>
</jboss-deployment-structure>