squick
squick

Reputation: 21

Why Java is slow when I connect to MySQL?

If I connect with Java to MySQL on my localhost server, I access instantaneously.
But if I connect outside of the localhost, from a network PC (192.168.1.100), it is very slow (4-5 seconds).
And, if I connect from a public IP to my MY SQL server, it is also very slow (6 seconds or more).

Upvotes: 2

Views: 3707

Answers (5)

Olha Puzhay
Olha Puzhay

Reputation: 370

4 seconds on connecting could be a DNS problem and cannot be just a pure network latency. Try starting MySQL server with "skip-name-resolve" parameter to skip resolving client's IP into hostname. Prior to that, make sure your grant tables are based on IPs and 'localhost' instead of symbolic names.

Upvotes: 1

Lee
Lee

Reputation: 13542

It will always take longer to make a connection across the network than to make the same connection locally. However, assuming you have a fairly typical local network, 4-5 seconds sounds a bit extreme. My guess (and it is just a guess) would be that the majority of the extra time is being consumed by network name resolution (i.e. DNS and/or netbios).

I would suggest that you try the connection using a numeric IP address, rather than a name.

Upvotes: 2

BalusC
BalusC

Reputation: 1108557

The "why" is already been answered. It's just the network latency.

You're probably also interested in how to "fix" it. The answer is: use a connection pool. If you're running a Java webapplication, use the webserver-provided connection pooling facilities. To take Tomcat as an example, check this manual. If you're running a Java desktop application, use a decent connection pool implementation like c3p0 (tutorial here).

Upvotes: 8

duffymo
duffymo

Reputation: 308733

Network latency plus connection creation time would be my guess. I don't know what else you have between the client machine and the MySQL server.

Upvotes: 1

BoltClock
BoltClock

Reputation: 723388

Because your computer needs time to send packets to external servers and they need time to send packets back. It's called network latency, and is not an issue with Java specifically, but a general network issue.

Upvotes: 6

Related Questions