问题:未设置 'hibernate.dialect' 时连接不能为空

Issue : Connection cannot be null when 'hibernate.dialect' not set

正在尝试通过数据源使用 Microsoft 驱动程序将 spring 代码连接到 SQL 服务器数据库。

申请-context.xml:

  <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
    <property name="driverClassName" value="${database.driverClassName}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
</bean>

属性 配置:

database.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
database.url=jdbc\:jtds\:sqlserver\://DB_ip:DB_Port/MyDB;instance=Some_Ins;integratedSecurity=true
database.username=
database.password=

错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
  ...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:296)

已编辑

Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
            at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97)
            at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67)
            at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:170)
            at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
          at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
            at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
            at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1797)
            at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1755)
            at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
            at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247)
            at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373)
            at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
            ... 39 more

无法确定根本原因..

当我的应用程序无法连接到数据库时,我遇到了同样的异常。所以即使 URL 错误,或者数据库名称错误,或者用户/密码组合无效 - 也会抛出相同的异常。

所以我想您应该验证连接属性 - 但要小心设置正确 - 从数据库服务器 url、端口、数据库名称、user/pass.

开始

在您的属性中,您有以下 2 个属性

database.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
database.url=jdbc\:jtds\:sqlserver\://DB_ip:DB_Port/MyDB;instance=Some_Ins;integratedSecurity=true

看起来您使用的是普通的 sql 服务器驱动程序而不是 jTDS 驱动程序,所以我至少希望 URL 没有 :jtds 部分。 url 中似乎也有不必要的斜杠。将 database.url 更改为以下

database.url=jdbc:sqlserver://DB_ip:DB_Port/MyDB;instance=Some_Ins;integratedSecurity=true

对于普通 SQL 服务器驱动程序,这应该是一个合适的 URL。