'hibernate.dialect' must be set when no Connection available

14,955

As far as I can tell, it is not possible to pass in the dialect as a value set on the hibernateProperties field of a Spring Session Factory, at least if you are also using the configLocation property on it.

My hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>

    <property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
    <property name="hibernate.search.autoregister_listeners">false</property>
    [etc...]

My relevant session factory context config:

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="dataSource" ref="dataSource"/>
       <property name="hibernateProperties">
        <props>
            <!--<prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>-->
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.generate_statistics">false</prop>
            <prop key="hibernate.default_schema">xxx</prop>
        </props>
    </property>
</bean>

If I uncomment the dialect prop in the context file, and comment it out from in the hibernate.cfg.xml file I encounter the same exception as the OP:

Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set

However if I run with the above configuration (commented out in the context file, uncommented in the hibernate.cfg.xml), it works, and I see the formatted hibernate SQL, showing that the other hibernate properties are being set by the context file.

Share:
14,955
Gleeb
Author by

Gleeb

Senior Back end developer at eXelate

Updated on June 04, 2022

Comments

  • Gleeb
    Gleeb about 2 years

    I am trying to run a hello world for: Spring/Hibernate with HSQLDB and C3PO connection pool. the same code works with mySQL (only with different dialect and driver)

    I have run the database and I can connect to it with the swing GUI. But when I try to run my application, I am getting a start up error. Here are the details:

    1: the error -

    INFO: Initializing Spring root WebApplicationContext [ERROR] [pool-2-thread-1 05:20:08] (JDBCExceptionReporter.java:logExceptions:101) Connections could not be acquired from the underlying database! [ERROR] [pool-2-thread-1 05:20:08] (ContextLoader.java:initWebApplicationContext:220) Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/hibernate-context.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection avalable at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) ... ...

    2: hibernate-context.xml -

    <tx:annotation-driven transaction-manager="transactionManager" />
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.gleeb.sample.model" />
        <property name="hibernateProperties">
            <props>
                <!-- <prop key="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> -->
                <prop key="dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="show_sql">false</prop>
                <prop key="hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>
    
            <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close" p:driverClass="org.hsqldb.jdbc.JDBCDriver"
        p:jdbcUrl="jdbc:hsqldb:hsql://localhost/testdb" p:user="sa"
        p:password="" p:acquireIncrement="5" p:idleConnectionTestPeriod="60"
        p:maxPoolSize="100" p:maxStatements="50" p:minPoolSize="10" />
    
    <!-- Declare a transaction manager -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory" />
    
  • Gleeb
    Gleeb about 12 years
    Amazingly it did something. not fix the problem but now i get: [ERROR] [pool-2-thread-1 06:03:04] (JDBCExceptionReporter.java:logExceptions:101) Connections could not be acquired from the underlying database!, but i am sure it has nothing to do with the real problem thats hiding under there.
  • Sudhakar
    Sudhakar about 12 years
    @gleeb Write a simple program using JDBC to connect to your HSQL DB and see if that works.That could atleast eliminate one issue
  • fredt
    fredt about 12 years
    Run the server with silent=false and check the connection attempt. Try p:user="SA"
  • Gleeb
    Gleeb about 12 years
    simple program using jdbc to connect to HSQLDB works like a charm. didn't even need to do anything i simply copied the connection string from the data source bean.
  • chalimartines
    chalimartines about 12 years
    According to this forum (because you have similiar situation) forum.springsource.org/archive/index.php/t-34157.html you should set maxPoolSize to lower value ... for example 10.
  • Gleeb
    Gleeb about 12 years
    Nop, sadly that didnt fix it. i got MaxPoolSize at 5 and min at 1. same thing happens.
  • chalimartines
    chalimartines about 12 years
    Can you try different datasource pool provider?
  • Gleeb
    Gleeb about 12 years
    is there any reason for a different connection pool?
  • chalimartines
    chalimartines about 12 years
    It can help us locate problem. We can check if problem is in c3p0 if it will be ok with another pool.