Closing of DB connections and exception handling

97 Views Asked by At

In my application I have a structure where methods are overloaded. One method does not have Connection as a parameter while the other one has. The one without is only used to create a database connection and then call the other one.

In this example, let's say i call the first method below to create a new user:

@Override
public User handleCreate(User item, boolean silent) throws DAOException {

    Connection conn = null;

    try {

        conn = daoFactory.getConnection();
        return handleCreate(conn, item, silent);

    } catch (SQLException ex) {
        rollback(conn);
        close(conn);
        throw new DAOException(ex);
    } finally {
        commit(conn);
        close(conn);
    }

}

and the other one that has Connection as a parameter:

@Override
public User handleCreate(Connection conn, User item, boolean silent) throws DAOException {
    try {

        boolean hasError = false;
        conn.setAutoCommit(false);

        item.setUsername(item.getUsername().trim());

        if (item.getId() == 0) {                                    // Not registered user

            if (userDAO.existByName(conn, item.getUsername())) {    // Username already exist

                msg.setErrorMessage(MessageHandler.getMessage("user.create.error.userAlreadyExist"));
                hasError = true;

            }

            if (userDAO.existEmail(conn, item.getEmail())) {        // Email already exist

                msg.setErrorMessage(MessageHandler.getMessage("user.create.error.emailAlreadyexist"));
                hasError = true;

            }

            if (!hasError) {                                        // No unique violations

                Integer id = userDAO.create(conn, item);                  // Create
                item.setId(id);
                msg.addMessage(MessageHandler.getMessage("user.create.completeMsg"));

            }

        } else {                                                    // Registered user

            msg.setErrorMessage(MessageHandler.getMessage("user.create.error.userAlreadySet"));

        }

        commit(conn);
        close(conn);

        return item;

    } catch (SQLException ex) {
        rollback(conn);
        close(conn);
        throw new DAOException(ex);
    } catch (DAOException ex) {
        rollback(conn);
        close(conn);
        throw ex;
    } finally {
        commit(conn);
        close(conn);
    }

}

What I'd like to ask about is what's best practice. I believe I'm overdoing the catching and I'm not sure how to reduce rollback, close etc. in the catch clauses without risking unhandled errors. I also would like to ask if it's best practice to commit and close the transaction/connection where you actually started it - in this case the first method.

Any insight to best practices in this would be much appreciated, or maybe you know another already answered question similar to mine that I haven't found.

1

There are 1 best solutions below

1
On

If there is an exception, there won't be any changes and you don't have to rollback. You can simply close the connection. Commit at the end is a good practice. If you want best practices, you will have to put a try catch against the close too. And I don't think you can pass connection as a parameter to rollback and commit.