GeeCON Prague 2014*
The moose likes JDBC and the fly likes SQLServerException: TCP/IP connection to the host has failed. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQLServerException: TCP/IP connection to the host has failed. " Watch "SQLServerException: TCP/IP connection to the host has failed. " New topic
Author

SQLServerException: TCP/IP connection to the host has failed.

Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
Hi,

I just migrated my java web application to a new laptop that has Window Vista on it. However, when I try to start up the Tomcat Server, I'm getting the following error:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:171)

On application start-up, I have data that I need to store in my application variables, so I have to make a database call to extract.

The following are what I'm using: newly installed MS SQL 2005, Hibernate, Spring and Struts.

I have no idea what is causing the error because I did the following:
1. Enabled TCP/IP (under the network's properies -> advance -> WIN tab)
2. Enabled port 1433 under the SQL Server protocol manager.
3. Test connect to the database via MyEclipse IDE (Database Explorer) using path: jdbc:sqlserver://localhost:1433;databaseName=RX. I was able to connect to the database successfully.

What I am doing wrong here? I never used Window's Vista or SQL 2005 before so I don't know what other configuration I need to make. Please Help.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Could be a firewall issue. Can you telnet to that port?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2501
    
    8

Could you try sqlserver://127.0.0.1:..... ?

Regards, Jan


OCUP UML fundamental and ITIL foundation
youtube channel
Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
Thanks Jan. I tried ip 127.0.0.1, but that did not work either.

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value></property>
<property name="url"><value>jdbc:sqlserver://127.0.0.1:1433;databaseName=RX</value></property>
<property name="username"><value>sa</value></property>
<property name="password"><value>admin</value></property>
</bean>
Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
....also I forgot to add that my FireWall is turned OFF.
George Stoianov
Ranch Hand

Joined: Jan 15, 2006
Posts: 94
Hi Nina,

One word of advice on SQL Server is make sure you are running the latest SP as MS is known for breaking its own stuff in the name of "security".

It seems like you are connecting to the loopback interface or localhost, so you may want to have a look at the sql server logs and whether your attempt actually gets registered there as the TCP IP error is indicative of a network problem. Please post the complete error message as well. And one more thing go over your network configuration in SQL Server again and post all properties you see with their values, make sure there is no list or other sort of limit on accepting connections from localhost, also make sure the server is not switching ports for some reason or using named pipes for localhost connections or something strange like that.

HTH
George
John
Greenhorn

Joined: Apr 23, 2008
Posts: 1
My problem is the same as yours. I spent all day crawling from site to site to find the answer but the result is still the same: "The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect". Enable Name pipes, TCP/IP, change Authentication mode, change localhost to 127.0.0.1 or ., add the Instance Name to the url, change the port, enable port and Apps in the firewall... almost everything. Its horrible! But the answer for MY problem is: open SQL Server Configuration Manager -> Protocols for SQL SQLEXPRESS, select Properties of TCP/IP. In the tab IP Addresses, set the TCPPort in section IPAll to 1433. And everthing 's OK.
Again, i would like to say: it's the answer to my problem and may be not yours.
Thanks!
Ramaswamy Srinivasan
Ranch Hand

Joined: Aug 31, 2004
Posts: 295
Other possible reasons could be,

1. The chosen port while installing the SQL Server might be already in use.

2. Permissions to the user name which is accessing the database might not have been granted.

3. If it a remote machine, remote settings are not enabled.
Matei Costescu
Greenhorn

Joined: Aug 25, 2008
Posts: 1
I had the same problem and I used the solution provided by John and it worked.

Thank you John.
Rajasekhar Pentakota
Ranch Hand

Joined: Aug 28, 2007
Posts: 38

John ... what ever solution provided you is really worth.. I have tried many ways provided in many blogs.. but couldn't make out.. but your solution has shown all the way.. Its general problem..

Thanks a lot!!!



Thanks
Raja
Rodney Hirokane
Greenhorn

Joined: Dec 04, 2009
Posts: 8
John, your solution worked fine. Thank you.
Jyothi kana
Greenhorn

Joined: Mar 25, 2010
Posts: 1
John wrote:My problem is the same as yours. I spent all day crawling from site to site to find the answer but the result is still the same: "The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect". Enable Name pipes, TCP/IP, change Authentication mode, change localhost to 127.0.0.1 or ., add the Instance Name to the url, change the port, enable port and Apps in the firewall... almost everything. Its horrible! But the answer for MY problem is: open SQL Server Configuration Manager -> Protocols for SQL SQLEXPRESS, select Properties of TCP/IP. In the tab IP Addresses, set the TCPPort in section IPAll to 1433. And everthing 's OK.
Again, i would like to say: it's the answer to my problem and may be not yours.
Thanks!



Hi john.. Your suggestion really helps me lot thanks...
Magesh Rajni
Greenhorn

Joined: Jul 06, 2010
Posts: 1
Thanks John. It works
Jk Harris
Greenhorn

Joined: Jul 13, 2010
Posts: 1
Thank you John. It worked. I forgot that above certain releases for security purposes that MS distributes software with stuff turned off as the default.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2501
    
    8

Welcome to JavaRanch, Magesh and Jk.
Tan Jee Shiuan
Greenhorn

Joined: May 25, 2011
Posts: 2
John, i followed your instructions but i still get the same error message. what should i do?
Tan Jee Shiuan
Greenhorn

Joined: May 25, 2011
Posts: 2
solved. 4get to restart it that's y still got the same error message
Pablo Quiroga
Greenhorn

Joined: Sep 24, 2011
Posts: 1
Thanks from Argentina John! You help me after hours of search. I only create an account to say this! Regards.
Steffen Meier
Greenhorn

Joined: Oct 28, 2011
Posts: 2
I don have the same problem. I can not connect to MS SQL SERVER anymore. A couple of days ago everything was fine. But then yesterday I was locked out. I was able to connect via Studio-application but not using JDBC via TCP/IP.

I reinstalled the server and did a lot ot make it work again. I was not able to see the SQL SERVER listening for incomming connection using netstat. Then I found this topic. It helped my to see the server socket to be open on TCP port 1433 again.

But I still can not connect to the DB successfully. The strange behaviour is now:
- if I use a login unknow to the database I get an error saying user ist unknown. The response is quite fast (about 2 seconds)
- if I use a valid login the call to DriverManager.getConnection(...) never returns.

When I stop the server the call to DriverManager.getConnection(...) returns with error: ???
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1352)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1339)
at com.microsoft.sqlserver.jdbc.TDSChannel.read(IOBuffer.java:1654)
at com.microsoft.sqlserver.jdbc.TDSReader.readPacket(IOBuffer.java:3694)
at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:5026)
at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:4978)

Any ideas what could be wrong? Unfortunately I can not switch to an other DBS like mysql ...

Pramod Kankure
Greenhorn

Joined: Oct 25, 2011
Posts: 6
Which version of JDBC driver you are using? You may want to upgrade it, if it is not a recent one.
Steffen Meier
Greenhorn

Joined: Oct 28, 2011
Posts: 2
I am using the very actual JDBC driver (sqljdbc4.jar) provided by MS.
Dean McNabb
Greenhorn

Joined: Oct 31, 2011
Posts: 2
Steffen Meier wrote:I am using the very actual JDBC driver (sqljdbc4.jar) provided by MS.


I too have this issue. My PC was just upgraded to Windows 7 (from XP). Being a company machine I don't have access to the Firewall to switch it off (and Windows Firewall is configured to popup if its blocking anything but does not popup - so it might be the driver itself).

Strangely enough, if I switch drivers to JTDS (and update the database connection urls) I don't have this problem.
Dean McNabb
Greenhorn

Joined: Oct 31, 2011
Posts: 2
I now know why this occurred. I had the latest JDK 6.0 update (29). I uninstalled this and installed update 27. Now the connection to SQL Server works as opposed to just "stopping".
spring lee
Greenhorn

Joined: Nov 01, 2011
Posts: 1
Dean McNabb wrote:I now know why this occurred. I had the latest JDK 6.0 update (29). I uninstalled this and installed update 27. Now the connection to SQL Server works as opposed to just "stopping".


Thanks a lot to Dean. Downgraded to JDK 6.0 update (27) solved my problem.
Vito Meuli
Greenhorn

Joined: Nov 08, 2011
Posts: 1
Dean McNabb wrote:I now know why this occurred. I had the latest JDK 6.0 update (29). I uninstalled this and installed update 27. Now the connection to SQL Server works as opposed to just "stopping".

Thank you very much Dean! I did the same and solved my problem.
Vito
Alan Douglas
Greenhorn

Joined: Nov 30, 2011
Posts: 2

New to the forum... I've run into a situation similar to this. My colleague is running NetBeans 7.1, as am I. JDK 1.7_01. He is on XP, I am on Windows 7. He is able to connect to SQL Server fine using the JDBC driver (sqljdbc4.jar). I am getting the described TCT/IP connection error. We double-checked we are using the same JDBC driver. Tried the same thing on a laptop running Windows 7, NB 7.1, JDK 1.7_01. Connection works. Tried the same thing on another Windows 7 box, NB 7.1, JDK 1.7_01. Same TCP/IP connection error.

Tried NB 6.7.1 running JDK 1.6_23. Unable to connect, just hangs there. Any ideas? I'm going to try on a different Windows 7 box with a fresh install of the OS, NB, etc.

Oh, I forgot to add... I am able to connect via the ODBC tool using the same credentials. Thanks.
Alan Douglas
Greenhorn

Joined: Nov 30, 2011
Posts: 2

I was able to finally determine my root cause to this... it had to do with the proxy settings in the NB IDE. I need to use a proxy, so I added the SQL Server host name to the 'No Proxy Hosts' list, and it now works.

Funny how sometimes when you find the solution, it becomes a 'duh' moment... should have looked at this long ago! Oh well, I learned a lot in the process!

Shankar Sajjan
Greenhorn

Joined: Mar 09, 2012
Posts: 1
im also facing the connection refused error, please help me.

thanks in advance
Premaa Kumar
Greenhorn

Joined: Sep 13, 2012
Posts: 2
Thanks John. I was also struggling from past 2-3 days regarding the sql server login issue and your instruction helped me resolved the issue. Thanks very much again.
 
GeeCON Prague 2014
 
subject: SQLServerException: TCP/IP connection to the host has failed.