mardi 8 juin 2021

How can I not throw exception at interface method signature If I throw exception in method that implements that interface?

I have Dao interfaces for each entity.Also I implement those interfaces for each resources, example MYSQL.MYSQL Dao methods throw specific exception, so I need throw them at interface layer , but Exceptions are specific for MYSQL, So how can I not throw It at Interface layer?Or Do I need change design? Example, I have UserDao Interface :

public interface UserDao {
    boolean insertUser(Connection connection, User user) throws MySQLEXContainer.MySQLDBNotUniqueException, MySQLEXContainer.MySQLDBLargeDataException, MySQLEXContainer.MySQLDBExecutionException, SQLException;
    boolean deleteUser(Connection connection,String login) throws MySQLEXContainer.MySQLDBExecutionException, SQLException;
    boolean validateUser(Connection connection,String login,String password) throws MySQLEXContainer.MySQLDBExecutionException, SQLException;
    User findUser(Connection connection,String login) throws MySQLEXContainer.MySQLDBExecutionException, SQLException;
    boolean updateUser(Connection connection,String login,String newPassword) throws MySQLEXContainer.MySQLDBExecutionException, SQLException;
    List<User> findAllUser(Connection connection) throws MySQLEXContainer.MySQLDBExecutionException, SQLException;
}

I have Implemented all of them, but I show only one

MYSQLUserDao class implementation of insertUser method

@Override
    public boolean insertUser(Connection connection, User user) throws MySQLEXContainer.MySQLDBNotUniqueException, MySQLEXContainer.MySQLDBLargeDataException, MySQLEXContainer.MySQLDBExecutionException, SQLException {
        LOGGER.debug("Insert User Is started");
        int rowNum;
        ResultSet keys = null;
        Connection con;
        PreparedStatement statement = null;
        try {
            String query = QueriesUtil.getQuery("insertUser");
            con = connection;
            statement = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
            statement.setString(1, user.getLogin());
            statement.setString(2, PasswordUtil.generateStrongPasswordHash(user.getPassword()));
            statement.setString(3, user.getUserType());
            statement.setString(4, user.getUserEmail());
            rowNum = statement.executeUpdate();
            keys = statement.getGeneratedKeys();
            if (keys.next()) {
                user.setUserId(keys.getInt(1));
            }
        } catch (SQLException e) {
            LOGGER.error(e);
            if (e.getErrorCode() == 1062) {
                throw new MySQLEXContainer.MySQLDBNotUniqueException(String.format("%s %s", user.getLogin(), user.getUserEmail()), e.getCause());
            } else if (e.getErrorCode() == 1406) {
                throw new MySQLEXContainer.MySQLDBLargeDataException("Data Is too long", e);
            }
            throw new MySQLEXContainer.MySQLDBExecutionException("Bad execution", e);
        } finally {
            ConnectionUtil.oneMethodToCloseThemAll(keys, statement, null);
            LOGGER.debug("Close all resources");
        }
        return rowNum > 0;
    }

If I want to implement dao for Oracle I will need to throw specific exception to OracleDb and therefore Interface's methods signature will have too many exceptions. How can I solve that problem?

Aucun commentaire:

Enregistrer un commentaire