From 1aba047f4c396566fe034936c5e7f590fd1ebfe0 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Sun, 31 Jul 2011 19:47:49 +0000 Subject: [PATCH] [#775] Automatic re-attaching after deserialisation does not work when used with .fetchLazy() --- .../java/org/jooq/impl/AbstractQuery.java | 13 +++- .../org/jooq/impl/AbstractResultQuery.java | 65 ++++++------------- 2 files changed, 33 insertions(+), 45 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractQuery.java b/jOOQ/src/main/java/org/jooq/impl/AbstractQuery.java index 90afbeacfd..3ef19c0bf5 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractQuery.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractQuery.java @@ -107,7 +107,10 @@ abstract class AbstractQuery extends AbstractQueryPart implements Query { return result; } finally { - JooqUtil.safeClose(statement); + if (!keepStatementOpen()) { + JooqUtil.safeClose(statement); + } + watch.splitDebug("Statement executed"); } } @@ -120,6 +123,14 @@ abstract class AbstractQuery extends AbstractQueryPart implements Query { } } + /** + * Default implementation to indicate whether this query should close the + * statement after execution. Subclasses may override this method. + */ + protected boolean keepStatementOpen() { + return false; + } + /** * Default implementation for query execution. Subclasses may override this * method. diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java b/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java index 37257c450c..cab11aa36f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java @@ -65,8 +65,9 @@ abstract class AbstractResultQuery extends AbstractQuery imple * Generated UID */ private static final long serialVersionUID = -5588344253566055707L; - private static final JooqLogger log = JooqLogger.getLogger(AbstractSelect.class); + private transient boolean lazy; + private transient Cursor cursor; private Result result; AbstractResultQuery(Configuration configuration) { @@ -94,7 +95,14 @@ abstract class AbstractResultQuery extends AbstractQuery imple } try { - result = executeLazy(configuration, statement).fetchResult(); + ResultSet rs = statement.executeQuery(); + FieldList fields = new FieldList(getFields(rs.getMetaData())); + cursor = new CursorImpl(configuration, fields, rs, statement, getRecordType()); + + if (!lazy) { + result = cursor.fetchResult(); + cursor = null; + } } finally { if (autoCommit) { @@ -102,7 +110,12 @@ abstract class AbstractResultQuery extends AbstractQuery imple } } - return result.size(); + return result != null ? result.size() : 0; + } + + @Override + protected final boolean keepStatementOpen() { + return lazy; } /** @@ -110,55 +123,19 @@ abstract class AbstractResultQuery extends AbstractQuery imple */ abstract boolean isSelectingRefCursor(); - private final Cursor executeLazy(Configuration configuration, Connection connection) throws SQLException { - StopWatch watch = new StopWatch(); - PreparedStatement statement = null; - - String sql = create(configuration).render(this); - watch.splitTrace("SQL rendered"); - - if (log.isDebugEnabled()) - log.debug("Lazy executing query", create(configuration).renderInlined(this)); - if (log.isTraceEnabled()) - log.trace("Preparing statement", sql); - - statement = connection.prepareStatement(sql); - watch.splitTrace("Statement prepared"); - - create(configuration).bind(this, statement); - watch.splitTrace("Variables bound"); - - Cursor cursor = executeLazy(configuration, statement); - watch.splitTrace("Statement executed"); - - return cursor; - } - - private final Cursor executeLazy(Configuration configuration, PreparedStatement statement) throws SQLException { - ResultSet rs = statement.executeQuery(); - - Class type = getRecordType(); - FieldList fields = new FieldList(getFields(rs.getMetaData())); - return new CursorImpl(configuration, fields, rs, statement, type); - } - @Override public final Result fetch() throws SQLException { execute(); - return getResult(); + return result; } @Override public final Cursor fetchLazy() throws SQLException { - Configuration configuration = attachable.getConfiguration(); - Connection connection = configuration.getConnection(); + lazy = true; + execute(); + lazy = false; - if (connection != null) { - return executeLazy(configuration, connection); - } - else { - throw new SQLException("Cannot execute query. No Connection configured"); - } + return cursor; } @Override