2

I am using hibernate to connect to the Microsoft SQL Server which is remotely installed. In the hibernate.cfg.xml file I have the following properties set:

<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

  • Check this answer for proper `hibernate.connection.url` format? http://stackoverflow.com/a/3588652/144432 Maybe lack of instance name is trying to make you log into some other database – Cengiz Can Mar 09 '12 at 00:15

2 Answers2

0

Code for creating session factory for hibernate framework using hibernate.cfg.xml

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.

<!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
  • Provided correct port, databasename , username and password
Mukesh Methaniya
  • 752
  • 1
  • 5
  • 13
0

Did you test your user name and password ? Exception tells that "Login failed for user 'user'." that means you have a connection to the server but given user name and password is not correct.

Koray Güclü
  • 2,857
  • 1
  • 34
  • 30
  • Sorry I should have told this before. I am able to connect to the remote machine using some tools like Navilite using my Windows authentication. So my user name and password are correct. – Raj Kumar Singh Mar 08 '12 at 23:10
  • So your Windows username is "user" and your password "password"? You cannot pass your Windows credentials like this. The database is created with username and password and you should supply those. – Jakub Zaverka Mar 08 '12 at 23:36
  • is your windows's username "user" and window's user password "pasword"? That seems highly unlikely. You should try putting your windows user name in yourwindowsusername and your windows password in yourwindowsuserpassword If you actually login into your compute using "user" and "password", then i dont know what the problem might be – Xavier Guzman Mar 08 '12 at 23:36
  • Maybe @RajKumarSingh just replaced real username and password values with those while posting here. – Cengiz Can Mar 09 '12 at 00:10
  • Yep, I simply replaced the "user" and "password". I think I know what is happening here. It seems that the guy in our IT support only allowed the Windows authentication for the database user. I read it here http://social.msdn.microsoft.com/Forums/en/sqldataaccess/thread/7b4eac48-4eee-43a3-81a7-de41440e846c. I think this should solve my problem. – Raj Kumar Singh Mar 09 '12 at 00:23