我有一个项目,我使用Ehcache技术的弹簧缓存。我也使用hibernate作为jpa提供程序。 我的请求方法调用创建了几个用于数据处理的线程。
当我在ehcache.xml中为我的服务模块的每个方法使用不同的缓存时,它运行良好,如下所示:
<cache name="rrr1"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="100000000"
eternal="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
overflowToDisk="false"
diskPersistent="false"
transactionalMode="off">
</cache>
<cache name="rrr2"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="100000000"
eternal="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
overflowToDisk="false"
diskPersistent="false"
transactionalMode="off">
</cache>
<cache name="rrr3"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="100000000"
eternal="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
overflowToDisk="false"
diskPersistent="false"
transactionalMode="off">
</cache>
但是当我尝试为我的方法设置单个缓存时,我遇到了这样的问题:
2016-08-05 11:12:11.385 INFO 46536 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.1.0.Final}
2016-08-05 11:12:11.387 INFO 46536 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2016-08-05 11:12:11.388 INFO 46536 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2016-08-05 11:12:11.426 INFO 46536 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2016-08-05 11:12:11.723 INFO 46536 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
2016-08-05 11:12:12.110 WARN 46536 --- [ main] o.h.b.i.SessionFactoryBuilderImpl : Unrecognized hbm2ddl_auto value : none. Supported values include create, create-drop, update, and validate. Ignoring
2016-08-05 11:12:12.667 INFO 46536 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2016-08-05 11:12:12.910 INFO 46536 --- [ main] o.s.c.ehcache.EhCacheManagerFactoryBean : Initializing EhCache CacheManager
2016-08-05 11:12:12.941 WARN 46536 --- [ main] n.sf.ehcache.config.CacheConfiguration : Statistics can no longer be enabled via configuration.
2016-08-05 11:12:13.560 INFO 46536 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@78691363: startup date [Fri Aug 05 11:12:08 NOVT 2016]; root of context hierarchy
2016-08-05 11:12:13.664 INFO 46536 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/greeting]}" onto public com.example.Greeting com.example.GreetingController.greeting(java.lang.String)
2016-08-05 11:12:13.666 INFO 46536 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-08-05 11:12:13.667 INFO 46536 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-08-05 11:12:13.715 INFO 46536 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-08-05 11:12:13.715 INFO 46536 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-08-05 11:12:13.772 INFO 46536 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-08-05 11:12:14.379 INFO 46536 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-08-05 11:12:14.457 INFO 46536 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-08-05 11:12:14.462 INFO 46536 --- [ main] com.example.DemoApplication : Started DemoApplication in 6.507 seconds (JVM running for 7.062)
2016-08-05 11:12:32.666 INFO 46536 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-08-05 11:12:32.666 INFO 46536 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2016-08-05 11:12:32.685 INFO 46536 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms
2016-08-05 11:12:33.162 INFO 46536 --- [ Thread-6] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.model.bs.Org
ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.model.bs.Org
ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.model.bs.Org
com.ric.bill.Standart和com.ric.bill.model.bs.Org - 我的实体
为什么会这样?
我的ehcache.xml(不工作):
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="cacheIO" />
<defaultCache maxElementsInMemory="1000000" eternal="true"
overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="0"
diskExpiryThreadIntervalSeconds="1800" memoryStoreEvictionPolicy="FIFO">
</defaultCache>
<cache name="org.hibernate.cache.internal.StandardQueryCache"
maxElementsInMemory="1000000" eternal="true" timeToLiveSeconds="0"
overflowToDisk="false" />
<cache
name="org.hibernate.cache.UpdateTimestampsCache"
maxEntriesLocalHeap="5000"
eternal="true"/>
<cache name="rrr1"
maxEntriesLocalHeap="1000000"
maxEntriesLocalDisk="1000000"
eternal="true"
diskPersistent="false"
diskSpoolBufferSizeMB="100"
timeToIdleSeconds="0" timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LFU"
overflowToDisk="false"
transactionalMode="off">
</cache>
<cache name="rrr2"
maxEntriesLocalHeap="1000000"
maxEntriesLocalDisk="1000000"
eternal="true"
diskPersistent="false"
diskSpoolBufferSizeMB="100"
timeToIdleSeconds="0" timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LFU"
overflowToDisk="false"
transactionalMode="off">
</cache>
<cache name="rrr3"
maxEntriesLocalHeap="1000000"
maxEntriesLocalDisk="1000000"
eternal="true"
diskPersistent="false"
diskSpoolBufferSizeMB="100"
timeToIdleSeconds="0" timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LFU"
overflowToDisk="false"
transactionalMode="off">
</cache>
</ehcache>
我的方法:
package com.ric.bill.mm.impl;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ric.bill.Calc;
import com.ric.bill.Storable;
import com.ric.bill.Utl;
import com.ric.bill.dao.ParDAO;
import com.ric.bill.excp.WrongGetMethod;
import com.ric.bill.mm.ParMng;
import com.ric.bill.model.bs.Dw;
import com.ric.bill.model.bs.Par;
@Service
public class ParMngImpl implements ParMng {
@Autowired
private ParDAO pDao;
@Cacheable(cacheNames="rrr1", key="{ #cd }")
public synchronized boolean isExByCd(String cd) {
Par p = getByCD(cd);
if (p != null) {
return true;
} else {
return false;
}
}
@Cacheable(cacheNames="rrr1", key="{ #st.getKlsk(), #cd, #genDt }")
public synchronized Double getDbl(Storable st, String cd, Date genDt) {
Par par = getByCD(cd);
try {
for (Dw d: st.getDw()) {
//по соотв.периоду
if (Utl.between(genDt, d.getDt1(), d.getDt2())) {
//проверка, что соответствует CD и Number (NM), Единичное (SI) - убрал - тормозит
/*if (checkPar(d.getFkHfp(), cd, "SI")) {
return d.getN1();
}*/
//if (d.getPar().getCd().equals(cd)) {
if (d.getPar().equals(par)) {
if (d.getPar().getTp().equals("NM")) {
if (d.getPar().getDataTp().equals("SI")) {
return d.getN1();
} else {
throw new WrongGetMethod("Попытка получить параметр "+cd+" не являющийся типом данного SI завершилась ошибкой");
}
} else {
throw new WrongGetMethod("Попытка получить параметр "+cd+" не являющийся типом NM завершилась ошибкой");
}
}
}
}
//если не найдено, то проверить, существует ли вообще этот параметр, в базе данных
if (!isExByCd(cd)) {
throw new WrongGetMethod("Параметр "+cd+" не существует в базе данных");
};
} catch (WrongGetMethod e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Cacheable(cacheNames="rrr1", key="{ #st.getKlsk(), #cd }")
public synchronized Double getDbl(Storable st, String cd) {
Par par = getByCD(cd);
try {
for (Dw d: st.getDw()) {
if (d.getPar().equals(par)) {
if (d.getPar().getTp().equals("NM")) {
if (d.getPar().getDataTp().equals("SI")) {
return d.getN1();
} else {
throw new WrongGetMethod("Попытка получить параметр "+cd+" не являющийся типом данного SI завершилась ошибкой");
}
} else {
throw new WrongGetMethod("Попытка получить параметр "+cd+" не являющийся типом NM завершилась ошибкой");
}
}
}
//если не найдено, то проверить, существует ли вообще этот параметр, в базе данных
if (!isExByCd(cd)) {
throw new WrongGetMethod("Параметр "+cd+" не существует в базе данных");
};
} catch (WrongGetMethod e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- Spring -->
<spring-framework.version>4.2.5.RELEASE</spring-framework.version>
<!-- Hibernate / JPA -->
<hibernate.version>5.1.0.Final</hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.samples</groupId>
<artifactId>Billing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle/ojdbc14 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>oracle</artifactId>
<version>10.2.0.2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.2.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>