Imri Persiado Imri Persiado - 6 months ago 12
SQL Question

java.net.ConnectException

I've seen few threads about this topic but I still can't figure out what's wrong.
Here is the code:

import java.sql.*;

public class SQL
{
public static void main(String[] args)
{
Connection conn = null;
String url = "jdbc:mysql://mysql1.oyo.co.il:3306/";
String dbName = "sdarot2_winner";
String driver = "com.mysql.jdbc.Driver";
String userName = "";
String password = "";
String table = "LEADER_CAM_PRODUCTS";

try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");

}catch (Exception e) {
System.out.println(e);
}
}
}


And here is the error I get:


com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying > > exception:

** BEGIN NESTED EXCEPTION **

java.net.ConnectException
MESSAGE: Connection timed out: connect

STACKTRACE:

java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.(Unknown Source)
at java.net.Socket.(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2744)
at com.mysql.jdbc.Connection.(Connection.java:1553)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at SQL.main(SQL.java:17)

** END NESTED EXCEPTION **

Last packet sent to the server was 1 ms ago.


Here is few things I found around:

a) The I.P/domain or port is incorrect

b) The I.P/domain or port (i.e service) is down

c) The I.P/domain is taking longer than your default timeout to respond

d) You have a firewall that is blocking requests or responses on whatever port you are using

e) You have a firewall that is blocking requests to that particular host

e) Your internet access is down

a) it is correct.
b) it's not down since I got a website that is working right now with that database.
c) how can I check that?I don't think that it's the problem since as I said I got a running website on this database.
d) now way because the same reason I mentioned above.
e) samem as d

So what I'm doing wrong?how come a php code can work with sql but java gives me errors?
Thank you.

Answer

As an experiment, try connecting to port 3306 on mysql1.oyo.co.il using your system's telnet command. Or some other utility (for example nc or netcat) that opens a raw TCP/IP connection. Depending on your OS / distro, you may have to locate and install a suitable command to help you with this trouble-shooting. Observe what happens.


Re your diagnosis:

a) it is correct.

If you say so ...

b) it's not down since I got a website that is working right now with that database.

At best you have demonstrated that the host is working. The web service on port 80 and the MySQL service on port 3306 are different services. (And in fact, it is possible that mysql1.oyo.co.il is doing clever packet routing tricks so that traffic on different ports is being tranparently routed to different hosts or virtual hosts.)

c) how can I check that?

Try changing / increasing the timeout.

I don't think that it's the problem since as I said I got a running website on this database.

My guess it is not a timeout issue ... but as I said above, the fact you got a website says NOTHING about whether the MySQL service is running.

d) now way because the same reason I mentioned above.

You've only demonstrated that you can get to port 80 ... see above

e) samem as d

You've only demonstrated that you can get to port 80 ... see above


Based on the above, it is plausible that:

  • you have a local firewall problem,
  • the MySQL service on that port is not currently running,
  • networking routing on the service side are broken for traffic on that port,
  • the MySQL service has been permanently turned off, or
  • the service's firewall has been configured to not accept network connections from random (or specific) places, including yours.

The fact that you are seeing a timeout rather than a "connection refused" suggests that this is more likely to be a networking or firewalling issue than a problem with the MySQL service endpoint itself.

My guess is that "they" have withdrawn the service ... and that you are not supposed to use it anymore. (Providing a public MySQL database service strikes me as an odd thing to do ... as well as being problematic from a security and administration standpoint.)