Apar
Apar

Reputation: 101

jsp mysql server connection timeout

hi i am doing an jsp project. and i deploy my project on apache tomcat. i use mysql as databese.

when i deploy project on remote server it is run good. but after some hours it gives me sql error. then i go back my apache server and start projecet again it run and after some hours it gives me same sql error again. i dont know the problem. is that caused from my java connection code or it is about mysql server. can some one tell me why it gives me sql error.?

public class ConnectionManager {

    private String className = "com.mysql.jdbc.Driver";
    private String userName ="username";
    private String password = "password";
    private String url = "jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8";
    /**
     * @uml.property  name="connectionInstance"
     * @uml.associationEnd  
     */
    private static ConnectionManager connectionInstance = null;

  public ConnectionManager(){

  }

  public static synchronized ConnectionManager getInstance() {
    if(connectionInstance == null) {
      connectionInstance = new ConnectionManager();         
    }
    return connectionInstance;
  }

  public Connection getConnection(){

      Connection conn = null;
      try {
          Class.forName(className);
          conn = DriverManager.getConnection (url, userName, password);
          System.out.println("Connection Established");
      }  catch (ClassNotFoundException e) {
          e.printStackTrace();
      }  catch (SQLException e) {
          e.printStackTrace();
      }
      return conn;
  }

Upvotes: 0

Views: 855

Answers (2)

BalusC
BalusC

Reputation: 1109222

MySQL has a default connection timeout of 8 hours. So this means that you've kept a SQL connection open for too long. Your code suggests that you're creating only one connection on application's startup and reusing it application wide. This is very bad. This is not threadsafe.

You need to change your code so that you're not declaring and storing the SQL Connection as a static or instance variable anywhere in your code. Instead, it should be declared, created and closed within the shortest possible scope. Preferably within the very same method block as where you're executing the SQL query.

Here's a minor rewrite of your ConnectionManager which does the job properly:

public class ConnectionManager {

    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String USERNAME ="username";
    private static final String PASSWORD = "password";
    private static final String URL = "jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8";

    static {
        try {
            Class.forName(DRIVER);
        }
        catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError(DRIVER + " missing in classpath!", e);
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }
}

Use it as follows:

public class SomeDAO {

    public SomeEntity find(Long id) throws SQLException {
        Connection connection = null;
        // ...

        try {
            connection = ConnectionManager.getConnection();
            // ...
        }
        finally {
            // ...
            if (connection != null) try { connection.close(); } catch(SQLException ignore) {}
        }

        return someEntity;
    }

To improve connecting performance, use a connection pool instead of DriverManager.

See also:

Upvotes: 1

Seshagiri
Seshagiri

Reputation: 748

Are you closing connections properly after using them.

Upvotes: 0

Related Questions