Overriding jOOQ's exception handling for Updat

2019-07-20 02:54发布

问题:

I'm using jOOQ v2.6 as I'm using SQL Server 2008 R2 and there is a bug in jOOQ v3.1 which causes code generation to fail. (I'm aware this will be fixed in v3.2).

From the manual:

// Create a new record
BookRecord book1 = create.newRecord(BOOK);

// Insert the record: INSERT INTO BOOK (TITLE) VALUES ('1984');
book1.setTitle("1984");
book1.store();

If store() fails, a DataAccessException is thrown. In my case I would simply like the process to sleep until either the CRUD operation works, or I observe the issue and intervene. This means that I need to wrap every instance of BookRecord.store() in a try/catch. This then applies to all CRUD operations, across all UpdatableRecords.

Is there a simple way that I can handle all CRUD DataAccessExceptions for all generated Record types, without having to remember to implement the same exception handler over and over again?

回答1:

I'm not 100% sure if this will meet your actual requirements, but using an ExecuteListener, you can hook into jOOQ's general query execution lifecycle and inject some behaviour into jOOQ's exception handling. Some examples are given here:

http://www.jooq.org/doc/3.1/manual/sql-execution/execute-listeners

In particular, your custom ExecuteListener might look like this:

public class MyListener extends DefaultExecuteListener {

    @Override
    public void exception(ExecuteContext ctx) {
        // Put some logic here
    }
}

Note, this currently won't prevent the throwing of the exception, itself.