Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 474
squick Avatar asked Sep 07 '26 23:09

squick


2 Answers

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).

like image 194
BalusC Avatar answered Sep 10 '26 13:09

BalusC


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.

like image 38
BoltClock Avatar answered Sep 10 '26 12:09

BoltClock