我正在尝试运行一个hello world:Spring / Hibernate with HSQLDB和C3PO连接池。 相同的代码适用于mySQL(只有不同的方言和驱动程序)
我已经运行了数据库,我可以通过swing GUI连接到它。但是当我尝试运行我的应用程序时,我遇到了启动错误。 以下是详细信息:
1:错误 -
INFO:初始化Spring root 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" />
答案 0 :(得分:1)
据我所知,不可能将方言作为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 prop,并在hibernate.cfg.xml文件中注释掉,我会遇到与OP相同的异常:
Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
但是,如果我运行上面的配置(在上下文文件中注释掉,在hibernate.cfg.xml中取消注释),它可以工作,我看到格式化的hibernate SQL,显示其他的hibernate属性是由上下文文件。
答案 1 :(得分: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>