0

I'm trying to use HikariCP for a Java EE app for school, but I don't understand why it doesn't work properly.. I'm making an API, and I'm using a MySQL Server with a Payara Server for my app.

I can call my API like 4 times before I get this error msg: http://prntscr.com/v7eopd The only way I found for solving is to restart Payara Server completely (even redeploy doesn't work)

I followed the HikariCP doc and configured my mysql.properties file as follow:

jdbcUrl = jdbc:mysql://mysql.iutrs.unistra.fr/devservweb?serverTimezone=UTC&useLegacyDatetimeCode=false
dataSource.user = user
dataSource.password = pwd
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
dataSource.useServerPrepStmts=true
dataSource.useLocalSessionState=true
dataSource.rewriteBatchedStatements=true
dataSource.cacheResultSetMetadata=true
dataSource.cacheServerConfiguration=true
dataSource.elideSetAutoCommits=true
dataSource.maintainTimeStats=false

I did a DAO Pattern so, here is my DAOFactory for MySQL

public class MySQLDAOFactory extends DAOFactory {
    private static final String PROPERTIES_FILE = "mysql.properties";

    public Connection getConnection() throws SQLException, DAOConfigurationException {
        Properties properties = new Properties();

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        InputStream propertiesFile = classLoader.getResourceAsStream(PROPERTIES_FILE);
        if(propertiesFile == null) {
            throw new DAOConfigurationException("File " + PROPERTIES_FILE + " not found");
        }

        try {
            properties.load(propertiesFile);
        } catch (IOException e) {
            throw new DAOConfigurationException("MySQL properties file could not be loaded " + PROPERTIES_FILE, e);
        }

        HikariConfig config = new HikariConfig(properties);
        config.setDriverClassName("com.mysql.cj.jdbc.Driver");
        HikariDataSource ds = new HikariDataSource(config);

        return ds.getConnection();
    }

    @Override
    public FlightDAO getFlightDAO() {
        return new MySQLFlightDAO(this);
    }

    @Override
    public UserDAO getUserDao() {
        return new MySQLUserDAO(this);
    }
}
public abstract class DAOFactory {
    public static DAOFactory getInstance(Persistence target) {
        DAOFactory daoF = null;

        switch (target) {
            case MySQL:
                daoF = new MySQLDAOFactory();
                break;
        }

        return daoF;
    }

    public abstract FlightDAO getFlightDAO();
    public abstract UserDAO getUserDao();
}
@Override
    public User findByUsername(String username) throws DAOException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        User user = null;

        try {
            connection = dao.getConnection();
            preparedStatement = initPreparedStatement(connection,
                    "SELECT user_id, username, password " +
                            "FROM user " +
                            "WHERE username = ?",
                    false,
                    username);

            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()) {
                user = map(resultSet);
            }

        } catch(SQLException e) {
            throw new DAOException(e);
        } finally {
            closes(resultSet, preparedStatement, connection); // I'm using connection.close, statement.close and resultset.close inside this method
        }

        return user;

Before using HikariCP I used the default JDBC for getting connection and my code worked properly.

I can't find how to solve it because I though HikariCP manage pool automatically...

I'm in a hurry because I have to send it to my professor at the end of the week, so if someone can help me please, I do not find any help on google :( ! Thanks !

Deewens
  • 107
  • 2
  • 11
  • The error is from DB. In mysql server check what is the max connection per user. Refer - https://stackoverflow.com/a/60434390/4158037. In your hikari CP, looks like you have not configured maxPoolSize. So default value of 10 will be taken. You need to ensure this maxPoolSize of hikari is <= max connection per user in DB. – Prasanna Oct 27 '20 at 05:50
  • Thanks it works now ! (sorry for the delay) – Deewens Nov 14 '20 at 16:34

0 Answers0