java - 使用 ehcache 时出错

标签 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)

应用程序.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
}

配置 hibernate :

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> 

在主要的 spring boot 应用程序中,我使用 @EnableCaching。 和我在实体上使用的注释:

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

最佳答案

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

执行此操作的方法在 documentation here 中.

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

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

关于java - 使用 ehcache 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42528910/

相关文章:

java - 在sql中查找上个月最后一天的数据

java - 如何使用 spring spEL 将 servlet 上下文路径注入(inject)到 bean 属性?

java - 在传递字段数组的实体中搜索字段

hibernate - junit 测试中的 Derby 序列循环

hibernate - 为什么createAlias在连接两个域的域上不起作用?

java - 如何使用两个对象中最特殊的类型作为返回类型?

java - springboot api 不工作。

java - 使用 saxon 将 XdmValue 作为 XmlType 存储在数据库中的更好方法。

java - 如何访问 JSP scriptlet 中的模型属性字段?

java - RabbitListenerTestHarness 在监听器中注入(inject)实际对象