尝试使用Microsoft驱动程序通过数据源将Spring代码连接到SQL Server数据库。
应用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$1.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
无法确定根本原因..
答案 0 :(得分:2)
在您的属性中,您有以下2个属性
database.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
database.url=jdbc\:jtds\:sqlserver\://DB_ip:DB_Port/MyDB;instance=Some_Ins;integratedSecurity=true
看起来你使用的是普通的sql server驱动程序而不是jTDS驱动程序,所以我至少会期望一个没有:jtds
部分的URL。此外,网址中似乎还有不必要的斜杠。将database.url
更改为以下
database.url=jdbc:sqlserver://DB_ip:DB_Port/MyDB;instance=Some_Ins;integratedSecurity=true
这应该是纯SQL Server驱动程序的正确URL。
答案 1 :(得分:1)
当我的应用程序无法连接到数据库时,我得到了相同的异常。因此,即使URL错误,或者数据库名称错误,或者用户/密码组合无效,也会抛出相同的异常。
所以我猜你应该验证连接属性 - 但要小心设置一切正确 - 从db server url,port,db name,user / pass开始。