【问题标题】:GWT (Jetty) + Hibernate 4.1.1 (JPA) + c3p0 throws ClassCastExceptionGWT (Jetty) + Hibernate 4.1.1 (JPA) + c3p0 抛出 ClassCastException
【发布时间】:2012-04-17 03:38:20
【问题描述】:

首先,我可以在Tomcat中成功调试应用程序而没有任何错误。当然,我无法在 Tomcat 中单步执行 GWT 客户端代码,因此我也尝试在 Jetty 上进行部署,这给了我这个选项。

因此,我正在尝试通过 Eclipse 中的 Jetty、使用 JPA 和 c3p0 的 Hibernate 4.1.1 调试使用 GWT (2.4.0) 的应用程序,但出现以下异常。

Caused by: java.lang.ExceptionInInitializerError
at SOMETHING.HibernateUtil.<clinit>(HibernateUtil.java:23)
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.service.jdbc.connections.spi.ConnectionProvider]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:186)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:150)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2273)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2269)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1738)
at SOMETHING.HibernateUtil.<clinit>(HibernateUtil.java:20)
... 29 more
Caused by: org.hibernate.HibernateException: Could not instantiate connection provider [org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider]
at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:192)
at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:114)
at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:54)
at org.hibernate.service.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:69)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:176)
... 41 more
Caused by: java.lang.ClassCastException: org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider cannot be cast to org.hibernate.service.jdbc.connections.spi.ConnectionProvider
at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:189)
... 45 more

我已经剥离了发生异常的信息,例如 RPC 调用异常。

我的 HibernateUtil 非常普通:

public class HibernateUtil {

private static final SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

static {
    try {
        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    } catch (Throwable ex) {
        // Log exception!
        throw new ExceptionInInitializerError(ex);
    }
}

public static Session getSession() throws HibernateException {
    return sessionFactory.openSession();
}

public static SessionFactory get() {
    return sessionFactory;
}
}

我的 hibernate.cfg.xml 是:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/dbname?autoReconnect=true</property>
    <property name="connection.username">dbuser</property>
    <property name="connection.password">dbpass</property>
    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
    <!-- Drop and re-create the database schema on startup -->
    <property name="hibernate.hbm2ddl.auto">update</property>
    <!-- hibernate.cfg.xml -->


    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">1800</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>
    <property name="hibernate.c3p0.idle_test_period">10</property>
    <property name="hibernate.c3p0.acquire_increment">5</property>
    <!-- no "connection.pool_size" entry! -->    
    <!-- JDBC connection pool (use the built-in) -->
    <!-- <property name="connection.pool_size">1</property> -->  
    <property name="hibernate.transaction.auto_close_session">false</property>

    <!-- Mapping files -->
            <!-- several mapping class files here -->
</session-factory>

我的 WebContent/WEB-INF/lib 文件夹中的库是:

antlr-2.7.7.jar
c3p0-0.9.1.2.jar
commons-collections-3.2.1.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
gwt-oauth2-0.2-alpha.jar
gwt-servlet.jar
hibernate-c3p0-4.1.1.Final.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.1.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
httpclient-4.1.3.jar
httpcore-4.1.4.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
log4j-1.2.14.jar
mysql-connector-java-5.1.18-bin.jar

在我的 Eclipse 构建路径中,hibernate-jpa-2.0-api-1.0.1.Final.jar 在顶部,然后是 hibernate-core-4.1.1.Final.jar。其余的是 Apache Tomcat 6、EAR、GWT SDK 2.4.0、JRE 1.6.0.26 和指向我的 lib 文件夹中所有库的 Web 应用程序库(包括 hibernate-jpa-2.0-api-1.0.1.Finalhibernate-core-4.1.1.Final)。我尝试在顶部包含 c3p0(休眠和/或实际库)但无济于事。同样的错误。还尝试了稍旧版本的 Hibernate 4 库,但同样的错误。

有什么想法吗?

【问题讨论】:

    标签: hibernate exception gwt jpa jetty


    【解决方案1】:

    我相信这是一个 Jetty 类加载器问题。当我尝试使用 c3p0 来解决由默认 ConnectionProvider 导致的 ClassCastException 时,我实际上收到了同样的问题,我相信这个问题应该有与 this 相同的解决方案。

    【讨论】:

    • 感谢马特,感谢您的及时回复。我在搜索过程中确实看到了这个错误报告和其他 SO 问题,但我不确定这段代码应该放在哪里,而且还提到了 EJB 和其他东西。有什么建议?再次感谢。
    • 我明白了。我应该覆盖 org.hibernate.service.classloading.internal 中的 ClassLoaderServiceImpl 类并注入我的代码,其中包含父级的 null。当然我现在遇到了其他怪物,比如Unable to build the default ValidatorFactoryUnable to find a default provider,但是ClassCastException错误消失了。你也遇到过这些吗?
    • 好的,一切就绪!我必须包括validation-api.1.0.0.GA.jarslf4j-api-1.6.1.jarslf4j-log4j12-1.6.1.jarhibernate-validator-4.2.0.Final.jar 才能让它快乐和快乐。 :)
    猜你喜欢
    • 2012-06-30
    • 1970-01-01
    • 2013-02-03
    • 2015-07-15
    • 1970-01-01
    • 2014-08-21
    • 2012-03-08
    • 2015-02-09
    • 1970-01-01
    相关资源
    最近更新 更多