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();
user3822347
  • 83
  • 2
  • 9
  • share minimal and complete testable code. – Braj Jul 26 '14 at 13:09
  • 1
    Your connection may be slow or the server might be heavily loaded – Darshan Lila Jul 26 '14 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. – user3822347 Jul 26 '14 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 – mkazma Jul 26 '14 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. – user3822347 Jul 26 '14 at 13:18
  • What do you mean with "700+ rows"? Your code does not retrieve anything from the database, so it's completely irrelevant how many rows the "database" contains. –  Jul 26 '14 at 14:12
  • I thought that there should not be any difference since I am not executing any query, but when the database was smaller I did not have to wait 5 seconds for the connection to establish. It's strange – user3822347 Jul 26 '14 at 14:23

3 Answers3

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.

user3822347
  • 83
  • 2
  • 9
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");
}
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
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).

leldo
  • 386
  • 2
  • 9
  • The database is relatively small. The connection works perfect with phpmyadmin. It only takes long to establish the Connection. – user3822347 Jul 26 '14 at 13:32