迁移到 Hibernate 4 后无法连接到数据库

Unable to connect to database after migrating to Hibernate 4

我正在尝试将休眠从 3.6.10.Final 更新到 4.3.6.Final。使用 3.6.10 一切正常。我将更改后的 类 重命名为新的类名并且在那一侧没有出现错误,但是当我 运行 网络应用程序时我收到错误:

2015-06-12 10:44:35.345 ERROR SchemaUpdate:201 - HHH000319: Could not get database metadata
java.sql.SQLException: Connections could not be acquired from the underlying database!

使用 hibernate 3.6.10 应用程序-context.xml 看起来像这样:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!--Annotation based Spring config -->
    <bean id="namingStrategy" class="org.hibernate.cfg.ImprovedNamingStrategy" />

    <context:component-scan base-package="nl.drs.esbcollector" />
    <bean
        class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <bean class="org.springframework.core.io.FileSystemResource">
                <constructor-arg>
                    <jee:jndi-lookup jndi-name="java:comp/env/ESBCollectorConf" />
                </constructor-arg>
            </bean>
        </property>
    </bean>

    <bean id="env" class="java.lang.String">
        <constructor-arg>
            <value>${general.env}</value>
        </constructor-arg>
    </bean>

    <!-- Datasource -->


    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${database.driver}"></property>
        <property name="jdbcUrl" value="${database.url}" />
        <property name="user" value="${database.username}" />
        <property name="password" value="${database.password}" />
        <property name="testConnectionOnCheckout" value="true" />
    </bean>


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="namingStrategy" ref="namingStrategy" />

        <property name="dataSource">
            <ref bean="dataSource" />
        </property>

        <property name="packagesToScan" value="nl.drs.esbcollector" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.jdbc.batch_size">50</prop>
                <prop key="hibernate.order_inserts">true</prop>
                <prop key="hibernate.order_updates">true</prop>
                <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
                <prop key="show_sql">false</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.c3p0.min_size">3</prop>
                <prop key="hibernate.c3p0.max_size">50</prop>
                <prop key="hibernate.c3p0.timeout">1800</prop>
                <prop key="hibernate.c3p0.idle_test_period">100</prop>
            </props>
        </property>

    </bean>


    <!-- Transactions -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="threadPoolExecutor"
        class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="5" />
        <property name="maxPoolSize" value="15" />
        <property name="keepAliveSeconds" value="60" />
    </bean>


    <!-- <bean name="cleanUpJob" class="org.springframework.scheduling.quartz.JobDetailBean">

        <property name="jobClass"
            value="nl.drs.esbcollector.controller.tasks.DataCleanUpJob" />

    </bean>
    Cron Trigger
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="cleanUpJob" />
        <property name="cronExpression" value="0/30 * * * * ?" />
    </bean>

    <bean id="schedulerFactory"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger" />
            </list>
        </property>
        <property name="applicationContextSchedulerContextKey">
            <value>applicationContext</value>
        </property>
    </bean> -->

    <bean name="complexJobDetail"    class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
    <property name="jobClass" value="nl.drs.esbcollector.controller.tasks.DataCleanUpJob" />

    <property name="durability" value="true" />
</bean>

<bean id="cronTrigger"  class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="complexJobDetail" />
    <property name="cronExpression" value="0/30 * * * * ?" />
</bean>


<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="jobDetails">
        <list>
            <ref bean="complexJobDetail" />
        </list>
    </property>

    <property name="triggers">
        <list>
            <ref bean="cronTrigger" />
        </list>
    </property>

    <property name="applicationContextSchedulerContextKey">
            <value>applicationContext</value>
        </property>
</bean> 


    <tx:annotation-driven />

</beans>

然后我将其更改为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!--Annotation based Spring config -->
    <bean id="namingStrategy" class="org.hibernate.cfg.ImprovedNamingStrategy" />

    <context:component-scan base-package="nl.drs.esbcollector" />
    <bean
        class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <bean class="org.springframework.core.io.FileSystemResource">
                <constructor-arg>
                    <jee:jndi-lookup jndi-name="java:comp/env/ESBCollectorConf" />
                </constructor-arg>
            </bean>
        </property>
    </bean>

    <bean id="env" class="java.lang.String">
        <constructor-arg>
            <value>${general.env}</value>
        </constructor-arg>
    </bean>

    <!-- Datasource -->


    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${database.driver}"></property>
        <property name="jdbcUrl" value="${database.url}" />
        <property name="user" value="${database.username}" />
        <property name="password" value="${database.password}" />
        <property name="testConnectionOnCheckout" value="true" />
    </bean>


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        <property name="dataSource" ref="dataSource" />


        <property name="packagesToScan" value="nl.drs.esbcollector" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.jdbc.batch_size">50</prop>
                <prop key="hibernate.order_inserts">true</prop>
                <prop key="hibernate.order_updates">true</prop>
                <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider
                </prop>
                <prop key="show_sql">false</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.c3p0.min_size">3</prop>
                <prop key="hibernate.c3p0.max_size">50</prop>
                <prop key="hibernate.c3p0.timeout">1800</prop>
                <prop key="hibernate.c3p0.idle_test_period">100</prop>
            </props>
        </property>

    </bean>


    <!-- Transactions -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="threadPoolExecutor"
        class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="5" />
        <property name="maxPoolSize" value="15" />
        <property name="keepAliveSeconds" value="60" />
    </bean>


    <!-- <bean name="cleanUpJob" class="org.springframework.scheduling.quartz.JobDetailBean"> 
        <property name="jobClass" value="nl.drs.esbcollector.controller.tasks.DataCleanUpJob" 
        /> </bean> Cron Trigger <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
        <property name="jobDetail" ref="cleanUpJob" /> <property name="cronExpression" 
        value="0/30 * * * * ?" /> </bean> <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
        <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> 
        <property name="applicationContextSchedulerContextKey"> <value>applicationContext</value> 
        </property> </bean> -->

    <bean name="complexJobDetail"
        class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass"
            value="nl.drs.esbcollector.controller.tasks.DataCleanUpJob" />

        <property name="durability" value="true" />
    </bean>

    <bean id="cronTrigger"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="complexJobDetail" />
        <property name="cronExpression" value="0/30 * * * * ?" />
    </bean>


    <bean id="schedulerFactory"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="complexJobDetail" />
            </list>
        </property>

        <property name="triggers">
            <list>
                <ref bean="cronTrigger" />
            </list>
        </property>

        <property name="applicationContextSchedulerContextKey">
            <value>applicationContext</value>
        </property>
    </bean>


    <tx:annotation-driven />

</beans>

简而言之

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

已更改为

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">

我是不是错过了一些导致现在连接失败的更改?

您需要删除以下属性:

<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider
<prop key="hibernate.c3p0.min_size">3</prop>
<prop key="hibernate.c3p0.max_size">50</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
<prop key="hibernate.c3p0.idle_test_period">100</prop>

Hibernate 已经使用外部 dataSource,所以它不使用 internal ConnectionProvider mechanism

您遇到异常,因为 Hibernate 可能会忽略正确配置的 dataSource 并尝试使用 C3P0ConnectionProvider,您没有为此提供 URLuser, password.