jsonb-api icon indicating copy to clipboard operation
jsonb-api copied to clipboard

Add jsonb.close() to all code examples in documentation

Open derekm opened this issue 5 years ago • 1 comments

Docs show using jsonb.fromJson() and jsonb.toJson() following Jsonb jsonb = JsonbBuilder.create(), but the docs are highly misleading in not showing the required jsonb.close() calls.

Update the docs to show the use of jsonb.close() or try-with-resources in all examples where a Jsonb instance is retrieved.

JSON-B implementations can memory-leak if close() is never called.

Johnzon is confirmed to leak if a local-scope Jsonb instance is never closed in a frequently used method (for example, in a MicroProfile Health implementation).

derekm avatar Feb 14 '20 21:02 derekm

In that vein, can we have a better exception for close than Exception?

From AutoCloseable.java:

/*
 * @apiNote
 * While this interface method is declared to throw {@code
 * Exception}, implementers are <em>strongly</em> encouraged to
 * declare concrete implementations of the {@code close} method to
 * throw more specific exceptions, or to throw no exception at all
 * if the close operation cannot fail.
 */

Handling Exception is very unsightly

        try (Jsonb jsonb = JsonbBuilder.create()) {
            return jsonb.fromJson(....);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

martinofcourse avatar Feb 17 '25 01:02 martinofcourse