Search in sources :

Example 1 with DetachedException

use of org.jooq.exception.DetachedException in project jOOQ by jOOQ.

the class AbstractQuery method execute.

@Override
public final int execute() {
    if (isExecutable()) {
        // Get the attached configuration of this query
        Configuration c = configuration();
        // [#1191] The following triggers a start event on all listeners.
        // This may be used to provide jOOQ with a JDBC connection,
        // in case this Query / Configuration was previously
        // deserialised
        DefaultExecuteContext ctx = new DefaultExecuteContext(c, this);
        ExecuteListener listener = ExecuteListeners.get(ctx);
        int result = 0;
        try {
            // [#8968] Keep start() event inside of lifecycle management
            listener.start(ctx);
            // [#385] If a statement was previously kept open
            if (keepStatement() && statement != null) {
                ctx.sql(rendered.sql);
                ctx.statement(statement);
                // [#3191] Pre-initialise the ExecuteContext with a previous connection, if available.
                ctx.connection(c.connectionProvider(), statement.getConnection());
                // [#6903] Increment and set the new statement execution count on re-execution
                ctx.withStatementExecutionCount(++statementExecutionCount);
            } else // [#385] First time statement preparing
            {
                listener.renderStart(ctx);
                rendered = getSQL0(ctx);
                ctx.sql(rendered.sql);
                listener.renderEnd(ctx);
                rendered.sql = ctx.sql();
                // ControlFlowSignals are thrown
                if (ctx.connection() == null)
                    if (ctx.configuration().connectionFactory() instanceof NoConnectionFactory)
                        throw new DetachedException("Cannot execute query. No JDBC Connection configured");
                    else
                        throw new DetachedException("Attempt to execute a blocking method (e.g. Query.execute() or ResultQuery.fetch()) " + "when only an R2BDC ConnectionFactory was configured. jOOQ's RowCountQuery and ResultQuery " + "extend Publisher, which allows for reactive streams implementations to subscribe to the " + "results of a jOOQ query. Simply embed your query in the stream, e.g. using Flux.from(query). " + "See also: https://www.jooq.org/doc/latest/manual/sql-execution/fetching/reactive-fetching/");
                listener.prepareStart(ctx);
                prepare(ctx);
                listener.prepareEnd(ctx);
                statement = ctx.statement();
            }
            // [#1856] [#4753] Set the query timeout onto the Statement
            int t = SettingsTools.getQueryTimeout(timeout, ctx.settings());
            if (t != 0)
                ctx.statement().setQueryTimeout(t);
            QueryPoolable p = SettingsTools.getQueryPoolable(poolable, ctx.settings());
            if (p == QueryPoolable.TRUE)
                ctx.statement().setPoolable(true);
            else if (p == QueryPoolable.FALSE)
                ctx.statement().setPoolable(false);
            if (// QueryParts may override this behaviour!
            executePreparedStatements(c.settings()) && // [#1520] Renderers may enforce static statements, too
            !TRUE.equals(ctx.data(DATA_FORCE_STATIC_STATEMENT))) {
                listener.bindStart(ctx);
                if (rendered.bindValues != null)
                    using(c).bindContext(ctx.statement()).visit(rendered.bindValues);
                listener.bindEnd(ctx);
            }
            result = execute(ctx, listener);
            return result;
        }// [#3427] ControlFlowSignals must not be passed on to ExecuteListners
         catch (ControlFlowSignal e) {
            throw e;
        } catch (RuntimeException e) {
            ctx.exception(e);
            listener.exception(ctx);
            throw ctx.exception();
        } catch (SQLException e) {
            ctx.sqlException(e);
            listener.exception(ctx);
            throw ctx.exception();
        } finally {
            // [#2385] Successful fetchLazy() needs to keep open resources
            if (!keepResultSet() || ctx.exception() != null) {
                Tools.safeClose(listener, ctx, keepStatement());
            }
            if (!keepStatement()) {
                statement = null;
                rendered = null;
            }
        }
    } else {
        if (log.isDebugEnabled())
            log.debug("Query is not executable", this);
        return 0;
    }
}
Also used : Configuration(org.jooq.Configuration) SQLException(java.sql.SQLException) QueryPoolable(org.jooq.conf.QueryPoolable) DetachedException(org.jooq.exception.DetachedException) ExecuteListener(org.jooq.ExecuteListener) ControlFlowSignal(org.jooq.exception.ControlFlowSignal)

Aggregations

SQLException (java.sql.SQLException)1 Configuration (org.jooq.Configuration)1 ExecuteListener (org.jooq.ExecuteListener)1 QueryPoolable (org.jooq.conf.QueryPoolable)1 ControlFlowSignal (org.jooq.exception.ControlFlowSignal)1 DetachedException (org.jooq.exception.DetachedException)1