Search code examples
javadatabase-connectionapache-commons-dbcp

Getting "connection timeout" instead of "connection refused"


I'm testing my application which requests some data from database. In case when the database is DOWN and installed on localhost I'm getting immediate Connection refused but when the database is running on remote machine Connection timeout happens. What could be the difference? I'm using apache BasicDataSource to get a connection.


Solution

  • Connection refused means service is not available at all. Refer to What can be the reasons of connection refused errors? post.

    While Connection timeout is a time period within which a connection between a client and a server must be established. If not established for some reason your code throws this exception after some retries. https://itstillworks.com/server-connection-timeout-mean-22703.html could give you some insights.

    localhost Vs remote:

    on remote machine the machine is acting as a proxy i.e. the machine is running but underlying service is not accessible via the network. While on localhost say localhost:port is down connection is straight away refused and no attempt of wait for connection is done.(Though if your machine had been up with the service but connection establishing was unsuccessful you would have got timeout )