Interface ResultProducer<R>

  • Type Parameters:
    R - Result type
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    public interface ResultProducer<R>
    Produces a result from an executed PreparedStatement.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      R produce​(java.util.function.Supplier<java.sql.PreparedStatement> statementSupplier, StatementContext ctx)
      Produces a statement result from a lazily supplied PreparedStatement.
    • Method Detail

      • produce

        R produce​(java.util.function.Supplier<java.sql.PreparedStatement> statementSupplier,
                  StatementContext ctx)
           throws java.sql.SQLException
        Produces a statement result from a lazily supplied PreparedStatement. The statement is not executed until statementSupplier.get() is invoked.

        Implementors that call statementSupplier.get() must ensure that the statement context is closed before returning, to ensure that database resources are freed:

         public R produce(Supplier<PreparedStatement> statementSupplier, StatementContext context) {
             ...
             try (StatementContext context = ctx) {
                 PreparedStatement statement = statementSupplier.get()
                 // generate and return result from the statement
             }
         }
         

        Alternatively, implementors may return some intermediate result object (e.g. ResultBearing or ResultIterable) without calling statementSupplier.get(), in which case the burden of closing resources falls to whichever object ultimately calls statementSupplier.get().

         public R produce(Supplier<PreparedStatement> statementSupplier, StatementContext context) {
             ...
             // the implementation of SomeOtherClass.createResponse which may call statementSupplier.get()
             // is now responsible to close the StatementContext.
             return SomeOtherClass.createResponse(statementSupplier, ..., ctx);
         }
         
        Parameters:
        statementSupplier - supplies a PreparedStatement, post-execution.
        ctx - the statement context
        Returns:
        an object of the type your caller expects
        Throws:
        java.sql.SQLException - if an error occurs while producing the result.