954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

MySQL + Java: SQL Exception: com.mysql.jdbc.CommunicationsException

Hi All,

This is the first time I'm using Java to connect to my online MySQL database that is hosted by a separate web host. Following is the code in use:
(I've removed the server url, username and pass for obvious reasons. They all work when I use them in my ASP web project.)

import java.sql.*;

/**
 *
 * @author Siddharth Dahiya
 */
public class TWMC_Online {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Starting...");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String connectionUrl = "jdbc:mysql://tribalwars.db.4489877.hostedresource.com";
            DriverManager.setLoginTimeout(100);
            Connection con = DriverManager.getConnection(connectionUrl, "xxxx", "xxxx");
            System.out.println("Con Done...");
        } catch (SQLException e) {
            System.out.println("SQL Exception: " + e.toString());
        } catch (ClassNotFoundException e) {
            System.out.println("Class Not Found Exception: " + e.toString());
        }
        System.out.println("Quiting...");
    }
}


I get the following error:

run:
Starting...
SQL Exception: 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(PlainSocketImpl.java:351)
	at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
	at java.net.Socket.connect(Socket.java:529)
	at java.net.Socket.connect(Socket.java:478)
	at java.net.Socket.<init>(Socket.java:375)
	at java.net.Socket.<init>(Socket.java:218)
	at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
	at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
	at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
	at java.sql.DriverManager.getConnection(DriverManager.java:582)
	at java.sql.DriverManager.getConnection(DriverManager.java:207)
	at twmc_online.TWMC_Online.main(TWMC_Online.java:23)


** END NESTED EXCEPTION **



Last packet sent to the server was 0 ms ago.
Quiting...
BUILD SUCCESSFUL (total time: 21 seconds)
sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

Are those your real login details? If yes, please edit them out.

Regarding timeout, are you able to ping the given host from your machine (ping tribalwars.db.4489877.hostedresource.com)? Also, what happens when you comment out the DriverManager.setLoginTimeout(100) line? I don't see a port number mentioned in there; is the db in consideration running on default MySQL port?

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Changed my passwords since the edit timeout has occured and I can't change it :(

Also, nothing happens when the timeout code is commented. The DB is running on the default port and I tried putting that in as well.

This is the result from the ping:

Pinging tribalwars.db.4489877.hostedresource.com [184.168.226.27] with 32 bytes of data: Request timed out. Reply from 184.168.226.27: bytes=32 time=137ms TTL=53 Reply from 184.168.226.27: bytes=32 time=136ms TTL=53 Reply from 184.168.226.27: bytes=32 time=134ms TTL=53

Ping statistics for 184.168.226.27: Packets: Sent = 4, Received = 3, Lost = 1 (25% loss), Approximate round trip times in milli-seconds: Minimum = 134ms, Maximum = 137ms, Average = 135ms

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

Is there a way to make it retry the connection? I set the timeout to 500Secs and it still timesout in like 5-7 secs...

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

> Is there a way to make it retry the connection

Automatic retries? Not that I'm aware of. Of course, it's dead simple to just loop over for a given number of times before giving up completely.

> I set the timeout to 500Secs and it still timesout in like 5-7 secs...

The timeout exception need not necessarily be of the client socket (in our case the socket used by the Driver). Server sockets can also have timeouts which might be the reason why you are getting time outs (which again is not surprising given timeouts in your ping). Are you able to connect to the database in consideration using some sort of SQL explorer like workbench ?

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 


I have used the MySQL GUI client to setup the database, so I'd say yes, I'm able to connect.

I Also tried to loop the try-catch block but got the same error twice :p

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

I was just going over my hosts description of details and this is what they say i need to connect to the server:
Host Name or Server
The name of the server your database is stored on. Don't know what this is? See Locating Your Database's Host Name for more information.

Port
If asked for the port, enter the following:
MySQL: 3306
MSSQL: 1433

User Name
The database's user name.

Password
The database's password. The password is created when the database is first set up but can be changed at any time.

Database
The database you want to connect to.

How do I send the Database name in my connection string?

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

A pity I missed that; you just need to append the database name at the end of your URL. Example here .

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Din't work... I changed the connection string to this:

String connectionUrl = "jdbc:mysql://tribalwars.db.4489877.hostedresource.com:3306/tribalwars";


Anyways, I have also posted a ticket with my host incase it is something on their side...

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

SO it was a problem with my host. Still thanks SOS for helping me out!!

sid78669
Junior Poster
198 posts since Nov 2008
Reputation Points: 10
Solved Threads: 8
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You