Buggy code:
public int executeUpdate ( String sql , String [ ] columnNames ) throws SQLException { } final void checkIfInMiddleOfBatch ( ) throws SQLException { } public boolean isClosed ( ) throws SQLException { } public final void close ( ) throws SQLException { } void closeActions ( ) throws SQLException { } public int getMaxFieldSize ( ) throws SQLException { } public void setMaxFieldSize ( int max ) throws SQLException { } public int getMaxRows ( ) throws SQLException { } public void setMaxRows ( int max ) throws SQLException { } public void setEscapeProcessing ( boolean enable ) throws SQLException { } public final int getQueryTimeout ( ) throws SQLException { } public final void setQueryTimeout ( int seconds ) throws SQLException { } public void cancel ( ) throws SQLException { } public SQLWarning getWarnings ( ) throws SQLException { } public void clearWarnings ( ) throws SQLException { } public void setCursorName ( String name ) throws SQLException { } public boolean execute ( String sql ) throws SQLException { } private boolean execute ( String sql , boolean executeQuery , boolean executeUpdate , int autoGeneratedKeys , int [ ] columnIndexes , String [ ] columnNames ) throws SQLException { } public boolean execute ( String sql , int autoGeneratedKeys ) throws SQLException { } public boolean execute ( String sql , int [ ] columnIndexes ) throws SQLException { } public boolean execute ( String sql , String [ ] columnNames ) throws SQLException { } public final ResultSet getResultSet ( ) throws SQLException { } public final int getUpdateCount ( ) throws SQLException { } public final boolean getMoreResults ( ) throws SQLException { } public final int getResultSetType ( ) throws SQLException { } public void setFetchDirection ( int direction ) throws SQLException { } public int getFetchDirection ( ) throws SQLException { } public void setFetchSize ( int rows ) throws SQLException { } public int getFetchSize ( ) throws SQLException { } public int getResultSetConcurrency ( ) throws SQLException { } public final int getResultSetHoldability ( ) throws SQLException { } public void addBatch ( String sql ) throws SQLException { } public final void clearBatch ( ) throws SQLException { } public int [ ] executeBatch ( ) throws SQLException { } boolean executeBatchElement ( Object batchElement ) throws SQLException , StandardException { } public final Connection getConnection ( ) throws SQLException { } public final boolean getMoreResults ( int current ) throws SQLException { } public final ResultSet getGeneratedKeys ( ) throws SQLException { } boolean executeStatement ( Activation a , boolean executeQuery , boolean executeUpdate ) throws SQLException { synchronized ( getConnectionSynchronization ( ) ) { setupContextStack ( ) ; boolean retval ; pvs = a . getParameterValueSet ( ) ; try { if ( ( results ) != null ) { results . close ( ) ; results = null ; } clearWarnings ( ) ; if ( ! ( forMetaData ) ) { commitIfNeeded ( ) ; needCommit ( ) ; } else { if ( ( lcc . getActivationCount ( ) ) > 1 ) { } else { commitIfNeeded ( ) ; needCommit ( ) ; } } PreparedStatement ps = a . getPreparedStatement ( ) ; ps . rePrepare ( lcc ) ; addWarning ( ps . getCompileTimeWarnings ( ) ) ; if ( ( cursorName ) != null ) { a . setCursorName ( cursorName ) ; } boolean executeHoldable = getExecuteHoldable ( ) ; a . setResultSetHoldability ( executeHoldable ) ; a . reset ( ) ; a . setMaxRows ( maxRows ) ; long timeoutMillis = ( ( long ) ( timeoutSeconds ) ) * 1000L ; org . apache . derby . iapi . sql . ResultSet resultsToWrap = ps . execute ( a , false , timeoutMillis ) ; addWarning ( a . getWarnings ( ) ) ; if ( resultsToWrap . returnsRows ( ) ) { if ( executeUpdate ) { throw StandardException . newException ( LANG_INVALID_CALL_TO_EXECUTE_UPDATE ) ; } EmbedResultSet lresults = factory . newEmbedResultSet ( getEmbedConnection ( ) , resultsToWrap , forMetaData , this , ps . isAtomic ( ) ) ; results = lresults ; if ( a . isSingleExecution ( ) ) <START_BUG> lresults . finalizeActivation = a ; <END_BUG> updateCount = - 1 ; retval = true ; } else { if ( ( a . getAutoGeneratedKeysResultsetMode ( ) ) && ( ( resultsToWrap . getAutoGeneratedKeysResultset ( ) ) != null ) ) { resultsToWrap . getAutoGeneratedKeysResultset ( ) . open ( ) ; autoGeneratedKeysResultSet = factory . newEmbedResultSet ( getEmbedConnection ( ) , resultsToWrap . getAutoGeneratedKeysResultset ( ) , false , this , ps . isAtomic ( ) ) ; } updateCount = resultsToWrap . modifiedRowCount ( ) ; resultsToWrap . finish ( ) ; results = null ; int dynamicResultCount = 0 ; if ( ( a . getDynamicResults ( ) ) != null ) { dynamicResultCount = processDynamicResults ( a . getDynamicResults ( ) , a . getMaxDynamicResults ( ) ) ; } if ( executeQuery && ( dynamicResultCount != 1 ) ) { throw StandardException . newException ( LANG_INVALID_CALL_TO_EXECUTE_QUERY ) ; } if ( executeUpdate && ( dynamicResultCount > 0 ) ) { throw StandardException . newException ( LANG_INVALID_CALL_TO_EXECUTE_UPDATE ) ; } if ( dynamicResultCount == 0 ) { if ( a . isSingleExecution ( ) ) { a . close ( ) ; } if ( ! ( forMetaData ) ) commitIfNeeded ( ) ; else { if ( ( lcc . getActivationCount ( ) ) > 1 ) { } else { commitIfNeeded ( ) ; } } } retval = dynamicResultCount > 0 ; } } catch ( Throwable t ) { if ( a . isSingleExecution ( ) ) { try { a . close ( ) ; } catch ( Throwable tt ) { } } throw handleException ( t ) ; } finally { restoreContextStack ( ) ; }

Fix:
lresults . singleUseActivation = a ;