MySQL Connection Limit Issue resolve

Standard

Follow following steps :

  1. vi /etc/my.cnf
  2. max_connections=2800
  3. save file
  4. service mysqld stop
  5. service mysqld start
  6. login into mysql and hit following command to check max connection limit :
    1. show variables like ‘max_connections’;
    2. Check “Threads_connected” variable to see total connections in use with following query :
      • show status like ‘%onn%’;

 

Solution in java code to not to increase MySQL connections :

  • In JDBC connection code strict to connection variable to not to create if already exists :

static Connection conn = null;
static Statement stmt = null;
constant cnst = new constant();
public dataBaseConnManager() {
if(conn==null)
{
conn = null;
stmt = null;
constant cnst = new constant();
// JDBC driver name and database URL
final String JDBC_DRIVER = “com.mysql.jdbc.Driver”;
final String DB_URL = “jdbc:mysql://localhost/”+cnst.dataBaseName;
final String USER = cnst.dataBaseUserName;
final String PASS = cnst.dataBasePassword;
try{
//STEP 2: Register JDBC driver
Class.forName(“com.mysql.jdbc.Driver”);
//STEP 3: Open a connection
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
stmt = conn.createStatement();

}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}