Spring + Hibernate + Sql Server连接失败 [英] Spring + Hibernate + Sql Server connection failing

查看:92
本文介绍了Spring + Hibernate + Sql Server连接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Spring,Hibernate和SQL Server的组合时,我收到以下错误。

I am getting the following error when I am using a combination of Spring, Hibernate and SQL Server.

19:17:09,137 ERROR [org.hibernate.tool.hbm2ddl.SchemaValidator] (MSC service thread 1-8) HHH000319: Could not get database metadata: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host falmumapp20/testdb, port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".

这可能不仅仅是一个TCP IP问题,因为如果我没有Spring工作,我能够使用hibernate连接到SQL Server。

It could not be just a TCP IP problem, because if I work without Spring, I am able to connect to SQL Server using hibernate.

下面是我的applicationContext.xml

Below is my applicationContext.xml



<!-- Resource Definition -->
<!-- Data Source Connection Pool -->
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    <property name="url" value="jdbc:microsoft:sqlserver://falmumapp20:1433" />
    <property name="username" value="tima"/>
    <property name="password" value="chalk@"/>
</bean>

<!-- Hibernate SessionFactory -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />

    <property name="mappingDirectoryLocations">
        <list>
            <value>classpath:/com/trun/hbm</value>
        </list>
    </property>
    <property name="configLocation">
       <value>classpath:hibernate.cfg.xml</value>
   </property>
    <property name="hibernateProperties">
        <props>
        <prop key="hibernate.hbm2ddl.auto">validate</prop>
        <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>

</bean>


<!--Definition of Transaction Manager-->
<bean id="myTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory" />
</bean>


<bean id="IDataService" class="com.trun.service.DataServiceImpl">
</bean>


<bean id="EventServlet" class="com.trun.servelet.LoginServelet">
    <property name="IDataDAO" ref="IDataDAO" />
</bean>

这是我的Hibernate配置file -

And here is my Hibernate Configuration file -

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
      <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
      <property name="hibernate.connection.url">jdbc:sqlserver://falmumapp20:1433;databaseName=testdb; user=tima;password=chalk@;</property>
      <property name="hibernate.connection.username">tima</property>
      <property name="hibernate.connection.password">chalk@</property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
      <property name="hibernate.current_session_context_class">thread</property>
      <property name="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
      <property name="hibernate.hbm2ddl.auto">validate</property>
      <!-- Mapping files -->

</session-factory>


推荐答案

您没有将数据库名称传递给连接URL

You are not passing database name to the connection url

<property name="url" value="jdbc:sqlserver://falmumapp20:1433;databaseName=testdb" />

<property name="url" value="jdbc:sqlserver://falmumapp20:1433/testdb" />

这篇关于Spring + Hibernate + Sql Server连接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆