Markus
Markus

Reputation: 407

Java jdbc timeout

I'm trying to connect to my database with a singelton class in java. This is my current code:

public class Database {

public static Database instance;

private Connection connection;

private Database() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Log.debug("ok");
        this.connection = DriverManager.getConnection("jdbc:mysql://just.not.for.you:3006/database?connectTimeout=5000", "database", "secret");
        Log.debug("ok");
    } catch (ClassNotFoundException e) {
        Log.warn("Cloud not find jdbc driver class");
    } catch (SQLException e) {
        Log.warn("Cloud not connect to database: " + e.getMessage(), e);
    }
}


public static synchronized Database getInstance() {
    if(Database.instance == null) {
        Database.instance = new Database();
    }
    return Database.instance;
}

In my main class i call Database.getInstance() but nothing happends. No error, no timeout message. It seems like an endless loop. I see the first debug message in line 10 but not the message in line 12.

What is the problem?

Edit: I can connect to the remote database with my local computer with mysql workbench

Upvotes: 0

Views: 551

Answers (1)

duyuanchao
duyuanchao

Reputation: 4303

I found some problem in your code!

  • Replace com.mysql.jdbc.Driver with new drivercom.mysql.cj.jdbc.Driver, because it is deprecated
  • Checkout mysql server port, default port is 3306 not 3006

Upvotes: 1

Related Questions