Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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" />
like image 471
Gleeb Avatar asked Nov 13 '22 06:11

Gleeb


1 Answers

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.

like image 164
slh777 Avatar answered Jan 13 '23 03:01

slh777