java Hibernate+c3p0池+ehcache失败启动
我有一个JavaFX应用程序,在添加2º缓存(带区域的ehcache)之前,它运行良好。之后,我启动应用程序,但在尝试创建会话工厂时出现以下错误:
08/10/2014 20:01:40 DEBUG Configuration:1841 - Preparing to build session factory with filters : {}
08/10/2014 20:01:40 INFO ConnectionProviderInitiator:190 - HHH000130: Instantiating explicit connection provider: org.hibernate.connection.C3P0ConnectionProvider
Exception in Application start method java.lang.reflect.InvocationTargetException
这些是我的文件(我输入了私人数据):
冬眠。cfg:
<property name="javax.persistence.validation.mode">none</property>
<property name="connection.release_mode">auto</property>
<property name="current_session_context_class">thread</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="show_sql">false</property>
<property name="format_sql">false</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">3000</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<!-- If I remove this line, I get an error which said something like region name is not defined --->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
POM
<?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">
<repositories>
<repository>
<id>terracotta-releases</id>
<url>http://www.terracotta.org/download/reflector/releases</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.8.4</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.33</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.5.Final</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.1.4.GA</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>2.0.2</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
</dependencies>
</project>
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">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskSpoolBufferSizeMB="30"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
statistics="false">
<persistence strategy="localTempSwap"/>
</defaultCache>
<cache name="region1"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="1000"
eternal="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
hibernateUtil
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public enum Util {
INSTANCE;
private final SessionFactory sessionFactory;
private Util() {
Configuration configuration;
StandardServiceRegistryBuilder builder;
configuration = new Configuration().configure();
builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(builder.build());
}
public static Util getInstance() {
return INSTANCE;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
}
当我尝试调试时,请在sessionFactory = configuration.buildSessionFactory(builder.build());
处停止,因为会话返回为null。但若我移除了池,会话就会工作
此外,在所有*。哈佛商学院。xml文件,我通过放置<cache usage="nonstrict-read-write" region="region1"/>
来设置缓存。若我删除了区域,则会启动,但会出现一个错误,因为我并没有定义一个区域,并且hibernate使用默认名称
# 1 楼答案
最后,我自己解决了这个问题。以下是答案:
冬眠。cfg
注意:如果在sessionFactory类中使用单例,则使用
SingletonEhCacheRegionFactory
类,如果不使用,则使用org.hibernate.cache.ehcache.EhCacheRegionFactory
一些最新版本的依赖项不能很好地与上述配置配合使用,所以在某些情况下我需要使用以前的版本
还有ehcache。具有缓存配置的xml:
注意:这个文件工作正常,但日志会报告警告,因为我没有为当前bean指定缓存,所以缓存采用默认配置。此外,我还收到了一些关于McChange依赖项文件的警告,mcach找不到这些文件,但实际上这不是一个错误,所以不要担心这种警告