Overriding jOOQ's exception handling for UpdatableRecords

大憨熊 提交于 2019-12-11 03:32:16

问题


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.



来源:https://stackoverflow.com/questions/18932960/overriding-jooqs-exception-handling-for-updatablerecords

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!