Dinesh Dabhi
Dinesh Dabhi

Reputation: 856

Too many connections in mysql in linux server

I have Web application in Cent Os server created in JSP. After some time the connections to mysql execeeds the limit and application could not work without restarting the mysql service.

Even I restarts the mysql and tomcat service some connections are allready created by mysql.

[root@localhost ~]# service tomcat restart

Shutting down Tomcat: Using CATALINA_BASE: /usr/local/apache-tomcat-7.0.27 Using CATALINA_HOME: /usr/local/apache-tomcat-7.0.27 Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.27/temp Using JRE_HOME: /usr/java/jre1.6.0_10 Using CLASSPATH: /usr/local/apache-tomcat-7.0.27/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.27/bin/tomcat-juli.jar

Starting Tomcat: Using CATALINA_BASE: /usr/local/apache-tomcat-7.0.27 Using CATALINA_HOME: /usr/local/apache-tomcat-7.0.27 Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.27/temp Using JRE_HOME: /usr/java/jre1.6.0_10 Using CLASSPATH: /usr/local/apache-tomcat-7.0.27/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.27/bin/tomcat-juli.jar

[root@localhost ~]#

service mysql restart

Shutting down MySQL. [ OK ]

Starting MySQL...... [ OK ]

[root@localhost ~]# netstat | grep mysql | wc -l

180

As shown above it shows 180 connections all ready running and the connections are like

[root@localhost ~]# netstat | grep mysql

tcp 1 0 localhost.localdomain:49058 localhost.localdomain:mysql CLOSE_WAIT tcp 1 0 localhost.localdomain:49061 localhost.localdomain:mysql CLOSE_WAIT tcp 1 0 localhost.localdomain:49060 localhost.localdomain:mysql CLOSE_WAIT tcp 1 0 localhost.localdomain:49063 localhost.localdomain:mysql CLOSE_WAIT tcp 1 0 localhost.localdomain:49062 localhost.localdomain:mysql CLOSE_WAIT

I have checked my sql files three times and there is no connections remained unclosed

Please give me the solution...

Upvotes: 0

Views: 1626

Answers (2)

Serhiy
Serhiy

Reputation: 4141

CLOSE_WAIT tcp socket state means that connection is in process if being closed, waiting for application to execute close() method. You should always execute close() method after finishing the usage of some resource (connection to database, file IO, network connections, etc).

You can check this example how to correctly handle your database connection.

Upvotes: 3

Tom Mac
Tom Mac

Reputation: 9853

It sounds to me as if you have a connection leak in your application somewhere.

One of the most common reasons for leaking connections in Java is to not include a finally in your try catch block.

Check out this post for more details.

Upvotes: 1

Related Questions