简体   繁体   English

休眠SQL Server连接

[英]Hibernate SQL Server Connection

I am using hibernate to connect to the Microsoft SQL Server which is remotely installed. 我正在使用休眠连接到远程安装的Microsoft SQL Server。 In the hibernate.cfg.xml file I have the following properties set: 在hibernate.cfg.xml文件中,设置了以下属性:

<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url">jdbc:sqlserver://machine:1433;databaseName=databaseName;</property>
    <property name="hibernate.connection.username">user</property>
    <property name="hibernate.connection.password">password</property>

The error message that I get in the log file is 我在日志文件中得到的错误消息是

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'user'. ClientConnectionId:cb5b8f60-5b4a-41ec-b67c-0784dc7f5d8f
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)
    at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:154)
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:173)
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:277)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)

Can someone please guide me as to what is going wrong? 有人可以指导我怎么了吗?

Thanks Raj 谢谢拉吉

Code for creating session factory for hibernate framework using hibernate.cfg.xml 使用hibernate.cfg.xml为hibernate框架创建会话工厂的代码

private static SessionFactory factory;
public void init() {
    try {
        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");

        factory = cfg.buildSessionFactory();

    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

Actual `hibernate.cfg.xml' content which need to add as resource in resource folder. 实际的“ hibernate.cfg.xml”内容,需要作为资源添加到资源文件夹中。

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- JDBC Database connection settings -->
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url">jdbc:sqlserver://localhost;port=1433;databaseName=XYZDb;instanceName=SQLEXPRESS;</property>
        <property name="connection.username">user</property>
        <property name="connection.password">password</property>
        <!-- JDBC connection pool settings ... using built-in test pool -->
        <property name="connection.pool_size">1</property>
        <!-- Select our SQL dialect -->
        <property name="dialect">org.hibernate.dialect.SQLServer2012Dialect</property>
        <!-- Echo the SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Set the current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- dbcp connection pool configuration -->
        <property name="dbcp.initialSize">5</property>
        <property name="dbcp.maxTotal">20</property>
        <property name="dbcp.maxIdle">10</property>
        <property name="dbcp.minIdle">5</property>
        <property name="dbcp.maxWaitMillis">-1</property>
    </session-factory>
</hibernate-configuration>

Make sure below point 确保以下几点

  • Used correct dialect according to your sql server version 根据您的SQL Server版本使用正确的方言
  • Provided correct port, databasename , username and password 提供正确的端口,数据库名称,用户名和密码

Did you test your user name and password ? 您是否测试了用户名和密码? Exception tells that "Login failed for user 'user'." 异常表明“用户'user'登录失败”。 that means you have a connection to the server but given user name and password is not correct. 这意味着您已经连接到服务器,但是给定的用户名和密码不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM