【问题标题】:'hibernate.dialect' must be set when no Connection available没有可用连接时必须设置“hibernate.dialect”
【发布时间】:2012-05-09 12:25:20
【问题描述】:

我正在尝试使用 HSQLDB 和 C3PO 连接池为 Spring/Hibernate 运行一个 hello world。 相同的代码适用于 mySQL(仅适用于不同的方言和驱动程序)

我已经运行了数据库,我可以使用 swing GUI 连接到它。但是当我尝试运行我的应用程序时,我遇到了启动错误。 以下是详细信息:

1:错误 -

INFO:初始化 Spring 根 WebApplicationContext [错误] [pool-2-thread-1 05:20:08] (JDBCExceptionReporter.java:logExceptions:101) 无法从底层数据库获取连接! [错误] [pool-2-thread-1 05:20:08] (ContextLoader.java:initWebApplicationContext:220) 上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建 ServletContext 资源 [/WEB-INF/hibernate-context.xml] 中定义的名称为“sessionFactory”的 bean 时出错:调用 init 方法失败;嵌套异常是 org.hibernate.HibernateException:没有可用连接时必须设置“hibernate.dialect” 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420) 在 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" />

【问题讨论】:

    标签: spring hibernate hsqldb c3p0


    【解决方案1】:

    我有带有 hibernate. 前缀的会话工厂属性。

    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.HSQLDialect
            hibernate.hbm2ddl.auto=update
            hibernate.show_sql=false
            hibernate.format_sql=false
        </value>
    </property>
    

    【讨论】:

    • 令人惊讶的是它做了一些事情。没有解决问题,但现在我得到:[ERROR] [pool-2-thread-1 06:03:04] (JDBCExceptionReporter.java:logExceptions:101) 无法从底层数据库获取连接!但我确定它与隐藏在其中的真正问题无关。
    • @gleeb 编写一个使用 JDBC 连接到 HSQL DB 的简单程序,看看是否可行。这至少可以消除一个问题
    • 使用silent=false 运行服务器并检查连接尝试。试试 p:user="SA"
    • 使用 jdbc 连接到 HSQLDB 的简单程序就像一个魅力。甚至不需要做任何事情,我只是从数据源 bean 复制了连接字符串。
    • 根据这个论坛(因为你有类似的情况)forum.springsource.org/archive/index.php/t-34157.html你应该将maxPoolSize设置为较低的值......例如10。
    【解决方案2】:

    据我所知,不可能将方言作为设置在 Spring Session Factory 的 hibernateProperties 字段上的值传递,至少如果您还使用了 configLocation 属性。

    我的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...]
    

    我的相关会话工厂上下文配置:

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

    如果我在上下文文件中取消注释 dialect 属性,并在 hibernate.cfg.xml 文件中将其注释掉,我会遇到与 OP 相同的异常:

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

    但是,如果我使用上述配置运行(在上下文文件中注释掉,在 hibernate.cfg.xml 中未注释),它可以工作,并且我看到格式化的休眠 SQL,表明其他休眠属性是由上下文文件。

    【讨论】:

      猜你喜欢
      • 2014-09-18
      • 2015-01-04
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      • 2014-06-29
      • 1970-01-01
      相关资源
      最近更新 更多