参数不匹配; lambda 表达式的无效功能描述符

argument mismatch; invalid functional descriptor for lambda expression

我正在像这样使用 jdbi 库:

try {
    return jdbi.withHandle(handle -> handle
        .createQuery("...")
        .mapTo(String.class)
        .one());
} catch (final JdbiException e) {
    if (DatabaseExceptionChecker.isTransientException(e)) {
        throw new MyCustomException("Transient database exception", e);
    } else {
        throw e;
    }
}

此 try catch 模式重复几次,将各种不同的查询传递给 jdbi.withHandle 方法。另一个例子:

try {
    return jdbi.withHandle(handle -> handle
        .createUpdate("...")
        .execute());
} catch (final JdbiException e) {
    if (DatabaseExceptionChecker.isTransientException(e)) {
        throw new MyCustomException("Transient database exception", e);
    } else {
        throw e;
    }
}

jdbi.withHandle 方法具有此签名:

public <R, X extends Exception> R withHandle(HandleCallback<R, X> callback) throws X {

我正在尝试找到一种方法来减少 try-catch 周围的重复,以便我可以使用类似这样的东西:

handleTransientExceptions(() -> jdbi.withHandle(handle -> handle
        .createQuery("...")
        .mapTo(String.class)
        .one())
}

handleTransientExceptions 函数将包含 try-catch 样板文件。

我从以下开始:

@FunctionalInterface
private interface JdbiCall
{
    public <R, X extends Exception> R call() throws X;
}

public <R, X extends Exception> R handleTransientExceptions(final JdbiCall jdbiCall) throws MyCustomException
{
    try {
        return jdbiCall.call();
    } catch (final JdbiException e) {
        if (DatabaseExceptionChecker.isTransientException(e)) {
            throw new MyCustomException("Transient database exception", e);
        } else {
            throw e;
        }
    }
}

然而,当我尝试这样称呼它时:

return handleTransientExceptions(() -> jdbi.withHandle(handle -> handle
        .createQuery("...")
        .mapTo(String.class)
        .one())
}

我收到错误:

  reason: cannot infer type-variable(s) R,X
    (argument mismatch; invalid functional descriptor for lambda expression
      method <R,X>()R in interface com.acmecorp.MyService.JdbiCall is generic)

无法推断类型,因为您的功能接口 JdbiCall 既没有 class 声明级别的泛型,也没有 call 可以推断这些类型的方法的参数.因此,您的 FI 可能如下所示:

@FunctionalInterface
interface JdbiCall<R, X extends Exception> {
    R call() throws X;
} 

handleTransientExceptions方法:

public <R, X extends Exception> R handleTransientExceptions(final JdbiCall<R, X> jdbiCall) throws Exception {
    try {
        return jdbiCall.call();
    } catch (final JdbiException e) {
        if (DatabaseExceptionChecker.isTransientException(e)) {
            throw new MyCustomException("Transient database exception", e);
        } else {
            throw e;
        }
    }
}

也可以简化为:

@FunctionalInterface
interface JdbiCall<R> {
    R call() throws Exception;
} 

handleTransientExceptions方法:

public <R> R handleTransientExceptions(final JdbiCall<R> jdbiCall) throws Exception {
    try {
        return jdbiCall.call();
    } catch (final JdbiException e) {
        if (DatabaseExceptionChecker.isTransientException(e)) {
            throw new MyCustomException("Transient database exception", e);
        } else {
            throw e;
        }
    }
}