Java – spring hibernate SQL Server connection failed

When I use the combination of spring, hibernate and SQL server, I receive the following error

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.".

It's not just a TCP IP problem, because if I don't work with spring, I can use hibernate to connect to SQL server

Here 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>

This is my hibernate profile –

<?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>

Solution

You did not pass the database name to the connection URL

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

or

<property name="url" value="jdbc:sqlserver://falmumapp20:1433/testdb" />
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>