Using JOOQ, how do you map an SQLException to a business exception?

Viewed 1224

I'm Using JOOQ, and would like to map certain SQLExceptions to business exceptions. The exception handling documentation page says:

The following section about execute listeners documents means of overriding jOOQ's exception handling, if you wish to deal separately with some types of constraint violations, or if you raise business errors from your database, etc.

However, on the page about execute listeners has no practical examples.

I know I have to implement ExecuteListener's method exception(ExecuteContext), but it's unclear to me whether I should throw another exception from there, or instead use the ExecuteContext.exception method to override the existing exception. Eg.

@Override
public void exception(ExecuteContext ctx) {
    if (ctx.sqlException().getSQLState().equals("23503")) {
        throw new ForeignKeyViolationException("Foreign key violation", ctx.sqlException());
    }
}

Or:

@Override
public void exception(ExecuteContext ctx) {
    if (ctx.sqlException().getSQLState().equals("23503")) {
        ctx.exception(ForeignKeyViolationException("Foreign key violation", ctx.sqlException()));
    }
}
1 Answers
Related