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

我试图将hibernate从3.6.10.Final更新到4.3.6.Final.随着3.6.10一切正常.我将更改的类重命名为新的类名并且没有在该方面出错,但是当我运行webapplication时出现错误:2015-06-12 10:44:35.345 ERROR SchemaUpdate:20...

我试图将hibernate从3.6.10.Final更新到4.3.6.Final.随着3.6.10一切正常.我将更改的类重命名为新的类名并且没有在该方面出错,但是当我运行webapplication时出现错误:

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,application-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,而您不提供URL,用户和密码.

本文标题为:java – 迁移到Hibernate 4后无法连接到数据库

基础教程推荐