Unable to connect to database after migrating to Hibernate 4

534 views Asked by At

I am trying to update hibernate from 3.6.10.Final to 4.3.6.Final. With 3.6.10 everything worked fine. I renamed the changed classes to the new classnames and get no errors on that side, but when I run the webapplication I get an error:

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!

With hibernate 3.6.10 the application-context.xml looked like this:

    <?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>

And I changed this to:

<?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>

In short

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

Has been changed to

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

And

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

to

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

Did I mis some change which is causing the connection to fail now?

1

There are 1 answers

2
Vlad Mihalcea On BEST ANSWER

You need to remove the following properties:

<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 already uses the external dataSource so it doesn't use the internal ConnectionProvider mechanism.

You are getting an exception, because Hibernate might ignore the properly configured dataSource and try to use the C3P0ConnectionProvider, for which you don't supply an URL, user, password.