Java and MySQL connection pool in Tomcat web application

I recently wrote and deployed a Java web application on a server, and I find an unusual problem that did not appear during development or testing.

When a user logs in after such a long time and proceeds to display data from the database, the page indicates that there are no records. But when you refresh the page, the first x-records are displayed in accordance with the pagination rules.

By checking the logs, I find:

ERROR|19 09 2009|09 28 54|http-8080-4|myDataSharer.database_access.Database_Metadata_DBA| - Error getting types of columns of tabular Dataset 12

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.io.EOFException

STACKTRACE:

java.io.EOFException
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1956)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2368)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2867)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1616)

And so on for a few hundred lines.

100 , . Apache Tomcat/jsps MySQL , , :

// Gets a Dataset.
public static Dataset getDataset(int DatasetNo) {
    ConnectionPool_DBA pool = ConnectionPool_DBA.getInstance();
    Connection connection = pool.getConnection();
    PreparedStatement ps = null;
    ResultSet rs = null;

    String query = ("SELECT * " +
                    "FROM Dataset " +
                    "WHERE DatasetNo = ?;");

    try {
        ps = connection.prepareStatement(query);
        ps.setInt(1, DatasetNo);
        rs = ps.executeQuery();
        if (rs.next()) {
            Dataset d = new Dataset();
            d.setDatasetNo(rs.getInt("DatasetNo"));
            d.setDatasetName(rs.getString("DatasetName"));
            ...

            }

            return d;
        }
        else {
            return null;
        }
    }
    catch(Exception ex) {
        logger.error("Error getting Dataset " + DatasetNo + "\n", ex);            
        return null;
    }
    finally {
        DatabaseUtils.closeResultSet(rs);
        DatabaseUtils.closePreparedStatement(ps);
        pool.freeConnection(connection);
    }
}

- ?

, , MySQL , .

'.


, , Oracle, , :

package myDataSharer.database_access;

import java.sql.*;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import org.apache.log4j.Logger;

public class ConnectionPool_DBA {

    static Logger logger = Logger.getLogger(ConnectionPool_DBA.class.getName());

    private static ConnectionPool_DBA pool = null;
    private static DataSource dataSource = null;


    public synchronized static ConnectionPool_DBA getInstance() {
        if (pool == null) {
            pool = new ConnectionPool_DBA();
        }
        return pool;
    }

    private ConnectionPool_DBA() {
        try {
            InitialContext ic = new InitialContext();
            dataSource = (DataSource) ic.lookup("java:/comp/env/jdbc/myDataSharer");
        }
        catch(Exception ex) {
            logger.error("Error getting a connection pool datasource\n", ex);
        }
    }

    public void freeConnection(Connection c) {
        try {
            c.close();
        }
        catch (Exception ex) {
            logger.error("Error terminating a connection pool connection\n", ex);           
        }
    }

    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        }
        catch (Exception ex) {
            logger.error("Error getting a connection pool connection\n", ex);            
            return null;
        }
    }    
}

, Oracle , .

+2
5

, , . Tomcat JNDI.

  • /, , . , 8 ( MySQL). - 15 (.. 15 , ). Tomcat JNDI removeAbandoned removeAbandonedTimeout, .
  • , , . , , , , Oracle "SELECT 1 FROM dual". Tomcat validationQuery MySQL - , "SELECT 1" ( ). , validationQuery, , , , .

, , , , . , , , .

Tomcat JNDI Commons DBCP, , DBCP, Tomcat.

+4

, ConnectionPool_DBA , Tomcat JNDI.

Oracle MySQL? JNDI , DBCP Apache. , .

, DatabaseUtils - , , .freeConnection() , .

, , SQL, Connection, , Connection. persistence , . , , , , .

UPDATE:

Google Oracle , . , - , . DBCP JNDI.

+3

, . :

  • MySQL ( 8 ). , , . , , , .

  • , . MySQL . , , , .

, . - ,

          ...
          removeAbandoned="true"
          removeAbandonedTimeout="120"
          logAbandoned="true"
          testOnBorrow="false"
          testOnReturn="false"
          timeBetweenEvictionRunsMillis="60000"
          numTestsPerEvictionRun="5"
          minEvictableIdleTimeMillis="30000"
          testWhileIdle="true"
          validationQuery="select now()"
+2

- , TCP/IP-?

, , -XX- - YY , .

0

In case you did not find the answer, I did this on the last day. I essentially do the same as you, except that I base my association with apache.commons.pool. Similarly, you see EOF. Check the mysqld error log file, which is most likely located in your data directory. Look for mysqld crash. mysqld_safe will quickly reload your mysqld if it works, so it won’t be obvious if you don’t look at its log file. / var / log does not help for this scenario.

Connections made before the crash will be EOF after the failure.

0
source

All Articles