2012-04-29 23 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属性正在由上下文文件设置。

相关问题