2012-04-29 21 views
5

我試圖運行hello world:Spring/Hibernate with HSQLDB和C3PO連接池。 相同的代碼與MySQL的工作(只有不同​​的方言和驅動程序)當沒有連接可用時,必須設置'hibernate.dialect'

我已經運行數據庫,我可以連接到它的擺動GUI。但是當我嘗試運行我的應用程序時,出現啓動錯誤。 下面是詳細信息:

1:錯誤 -

INFO:初始化彈簧根的WebApplicationContext [錯誤] [池-2-線程1 5時20分08秒](JDBCExceptionReporter.java: logExceptions:101)無法從底層數據庫獲取連接! [錯誤] [pool-2-thread-1 05:20:08](ContextLoader.java:initWebApplicationContext:220)上下文初始化失敗 org.springframework.beans.factory.BeanCreationException:創建bean名稱爲'sessionFactory'時定義錯誤在ServletContext資源中[/WEB-INF/hibernate-context.xml]:調用init方法失敗;當org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420) 位於org.springframework.beans時,必須設置'hibernate.dialect'。 .factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) ... ...

2:冬眠-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" /> 

回答

0

我有會話工廠性質與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> 
+0

令人驚訝的是,它做了一些事情。沒有解決問題,但現在我得到: [錯誤] [池2線程1 06:03:04](JDBCExceptionReporter.java:logExceptions:101)連接無法從底層數據庫獲得!,但我是確定它與隱藏在那裏的真正問題無關。 – Gleeb 2012-04-29 15:04:13

+0

@gleeb使用JDBC編寫一個簡單的程序連接到您的HSQL DB,看看是否有效。這可以至少消除一個問題 – Sudhakar 2012-04-29 15:09:23

+0

用silent = false運行服務器並檢查連接嘗試。試用p:user =「SA」 – fredt 2012-04-29 21:31:32

1

據我所知,這是不可能的方言作爲一個春季會議廠的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> 

如果我取消的背景下,文件中的方言道具,以及在其註釋掉hibernate.cfg.xml文件我遇到與OP相同的異常:

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

但是,如果我運行e上面的配置(註釋掉在上下文文件中,未在hibernate.cfg.xml中註釋),它工作,我看到格式化的hibernate SQL,顯示其他hibernate屬性正在由上下文文件設置。

相關問題