1

I am trying to connect to the mysql server, but this takes 5 seconds.

showTime();
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.1.212:3306/db",username.getText(),password.getText());
showTime();
7
  • share minimal and complete testable code. Commented Jul 26, 2014 at 13:09
  • 1
    Your connection may be slow or the server might be heavily loaded Commented Jul 26, 2014 at 13:10
  • with almost empty database ~100 rows it is relatively quick. With 700+ rows it takes 5 seconds or more only to connect. Commented Jul 26, 2014 at 13:12
  • Can you show us some code of what you are trying to do here so that we can figure out what's taking too long ? maybe it's not in the connection setup Commented Jul 26, 2014 at 13:15
  • I'm just creating the layout for a JFrame, nothing else.The code above is in an Action Listener. Between the two showTime() is a 5 second pause. Commented Jul 26, 2014 at 13:18

3 Answers 3

2

My problem was actually with the mysql server. I was running Windows Server 2008R2, and after I exhausted all possible answers I decided to uninstall windows and install Ubuntu Server. After that, the same exact snippet of code worked perfectly.

Sign up to request clarification or add additional context in comments.

Comments

0

Braj is right about connection pooling but in case your application is not executed in a heavy container and in case is just a tiny database access, I advise you to test your connexion with a mysql client to figure out which part is slow (your application or the DBMS itself).

1 Comment

The database is relatively small. The connection works perfect with phpmyadmin. It only takes long to establish the Connection.
0

I suggest you to use Connection Pool where connections are already created and stored in the pool instead of creating at run-time.

In software engineering, a connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required.

Connection pools are used to enhance the performance of executing commands on a database.

I have already shared a nice ConnectionUtil class to manage all the connections in a single class for whole application.

Use JNDI to bind the data-source with the application rather than hard-coding username/password and configuration in the java class itself.

It's better explained under Java Tutorial on Connecting with DataSource Objects

DataSource objects can provide connection pooling and distributed transactions. This functionality is essential for enterprise database computing.

There is one more post on StackOverflow Why do we use a DataSource instead of a DriverManager? that explain it as well.


Move the driver class loading step in the Static Initialization Block

static{
    Class.forName("com.mysql.jdbc.Driver");
}

5 Comments

I'm doing this at the log-in point. Throughout the classes I pass this Connection and it works fine.
don't you think that data-source must be loaded before application start to server the request.
I just changed that, still 5 seconds :(
I just tried it, unfortunately I have the same result
I just tried it with DataSource and it still takes 5 seconds to establish a connection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.