N
N
NoMoneyException2017-05-04 16:48:40
Java
NoMoneyException, 2017-05-04 16:48:40

Should the DAO layer check for possible errors?

Hello. I have a DAO layer where I work with the database. There are methods something like createUser(). Take a look:

public boolean createUser(User user) throws DAOException {

        ConnectionPool connectionPool = ConnectionPoolImpl.getInstance();

        try (Connection connection = connectionPool.getConnection()) {
            final String CREATE_USER_SQL = "INSERT INTO hostel.user" +
                    "(`login`," +
                    "`password`," +
                    "`first_name`," +
                    "`last_name`," +
                    "`sex`," +
                    "`country`," +
                    "`city`," +
                    "`registration_date`," +
                    "`date_of_birth`," +
                    "`phone_number`," +
                    "`discount_id`," +
                    "`account_status_id`)" +
                    "VALUES (?, (SELECT SHA2(?, 256)), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";


            try (PreparedStatement preparedStatement =
                         connection.prepareStatement(CREATE_USER_SQL)) {

                preparedStatement.setString(1, user.getLogin());
                preparedStatement.setString(2, user.getPassword());
                preparedStatement.setString(3, user.getFirstName());
                preparedStatement.setString(4, user.getLastName());
                preparedStatement.setBoolean(5, user.isSex());
                preparedStatement.setString(6, user.getCountry());
                preparedStatement.setString(7, user.getCity());
                preparedStatement.setDate(8, new Date(user.getRegistrationDate().getTime()));
                preparedStatement.setDate(9, new Date(user.getDateOfBirth().getTime()));
                preparedStatement.setInt(10, user.getPhoneNumber());
                preparedStatement.setInt(11, user.getDiscountId());
                preparedStatement.setInt(12, user.getAccountStatusId());

                return preparedStatement.executeUpdate() > 0;
            }
        } catch (SQLException e) {
            logger.log(Level.ERROR, e);
            throw new DAOException("Error while executing the query.");
        } catch (ConnectionPoolException e) {
            logger.log(Level.ERROR, e);
            throw new DAOException("Error trying to work with the connection pool.", e);
        }

    }

The question is. User = null arrives - will be expedited, user arrives with null fields - will be expedited, user arrives with invalid data (for example, non-existent discount_id - this is a foreign key) - expedited. I can and am going to check all this and exclude it in the service layer, but will it be right? Or is it all the same to check it all again on the DAO? Then it turns out too much logic in it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-05-08
@dsv

1. It is not forbidden to check input parameters and this does not apply to business logic. Those. checks won't hurt.
2. Despite the checks of item 1, the input parameters must be correct and not cause errors, i.e. in calling methods of the service layer, User objects must be correctly formed in any case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question