0

I am getting this error when i log into my program and leave it for a minute or so:

com.mysql.jdbc.exceptions.jdbc4.CommuncationsException: Communications link failure
The last packet succesfully received from the server was 590,905 miliseconds ago. The last packet sent succesfully to the server was (cant remember correct number) miliseconds ago.

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.

The connection to the database ends here and my program no longer works. Here is my connection to mysql db config file:

import java.sql.*;
import javax.swing.*;
public class mysqlconnect {
    Connection conn = null;
    public static Connection ConnectDb()    {
        try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://MYSERVER.COM/MY_DATABASE","USERNAME","PASSWORD");
            //JOptionPane.showMessageDialog(null, "Connection successfull");
            return conn;
        }catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Cant connect to db");
            return null;
        }
    }
}

After doing some research i think i need to add more configurations to this file, but not sure what to add or even if i really need to modify this file. Any suggestions?

Original code with only wrong username password:

import java.sql.*;
import javax.swing.*;
public class mysqlconnect {
    Connection conn = null;
    public static Connection ConnectDb()    {
        try{
            Class.forName("com.mysql.jdbc.Driver");
            //Connection conn = DriverManager.getConnection("jdbc:mysql://rude.su.lt/data_base1","USERNAME","PASSWORD");
            Connection conn = DriverManager.getConnection("jdbc:mysql://rude.su.lt/data_base1","USERNAME","PASSWORD");
            //JOptionPane.showMessageDialog(null, "Prisijungimas pavyko");
            return conn;
        }catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Prisijungimas prie duomenų bazės nepavyko");
            return null;
        }
    }
}
Edgar
  • 1,113
  • 3
  • 16
  • 30
  • 1
    Your connection is probably timing out - check out http://stackoverflow.com/questions/1683949/connection-timeout-for-drivermanager-getconnection – NG. Apr 23 '13 at 17:23

2 Answers2

0

Add autoReconnect to MySql DB Connection URL ,,, It reconnects automatically when connection is lost

Check the connection code below:

Connection conn = DriverManager.getConnection("jdbc:mysql://MYSERVER.COM/MY_DATABASE?autoReconnect=true","USERNAME","PASSWORD"); 

For more MySQL Connection Parameters check this link: http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html

Also check that you don't close Connection or Statement unless you finish executing DML or DLL

Note: In your code it seems that you have missed the port of mysql 3306 (if you didn't changed the default one)

Connection Code Update:

Connection conn = DriverManager.getConnection("jdbc:mysql://MYSERVER.COM:3306/MY_DATABASE?autoReconnect=true","USERNAME","PASSWORD");
0

I am suggesting you to implement a Connection Pooling library to handle Database connection. It's not recommended to use the autoReconnect flag to re-establish dead/stale connections.

In any case, there is no 100% safe way that a JDBC driver can re-connect automatically if a TCP/IP connection dies without risking corruption of the database 'state' (even with transactional semantics), which is why this feature will eventually be removed. Source

Below is an example based on c3p0 connection pooling library.

ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver            
cpds.setJdbcUrl( "jdbc:mysql://MYSERVER.COM/MY_DATABASE:MY_PORT");
cpds.set
cpds.setUser("dbuser");                                  
cpds.setPassword("dbpassword");

Below line would return you a connection from the pool -

cpds.getConnection(); 

For refer further at c3p0 docs.

Community
  • 1
  • 1
Chan
  • 2,601
  • 6
  • 28
  • 45