Index: java/drda/org/apache/derby/impl/drda/DRDAResultSet.java
===================================================================
--- java/drda/org/apache/derby/impl/drda/DRDAResultSet.java	(revisjon 394134)
+++ java/drda/org/apache/derby/impl/drda/DRDAResultSet.java	(arbeidskopi)
@@ -439,7 +439,7 @@
 	 *  column; false otherwise.
 	 ****/
  
-	private boolean hasLobColumns()	throws SQLException
+	protected boolean hasLobColumns() throws SQLException
 	{
 		ResultSetMetaData rsmd = rs.getMetaData();
 		int ncols = rsmd.getColumnCount();
@@ -562,5 +562,15 @@
 		this.qryrowset = qryrowset;
 		this.qryclsimp = (qryclsimpl == CodePoint.QRYCLSIMP_SERVER_CHOICE)
 			? DRDAResultSet.QRYCLSIMP_DEFAULT : qryclsimpl;
+
+		// Assume that we are returning data until a CNTQRY command
+		// tells us otherwise. (DERBY-822)
+		qryrtndta = true;
+
+		// For scrollable result sets, we don't know the fetch
+		// orientation until we get a CNTQRY command. Set orientation
+		// and row number to make pre-fetching possible. (DERBY-822)
+		qryscrorn = CodePoint.QRYSCRREL;
+		qryrownbr = 1;
 	}
 }
Index: java/drda/org/apache/derby/impl/drda/DRDAStatement.java
===================================================================
--- java/drda/org/apache/derby/impl/drda/DRDAStatement.java	(revisjon 394134)
+++ java/drda/org/apache/derby/impl/drda/DRDAStatement.java	(arbeidskopi)
@@ -306,13 +306,25 @@
 	
 
 	/**
-	 * Delegation method to call DRDAResultSet to set query 
-	 * options sent on OPNQRY.
+	 * Set query options sent on OPNQRY and pass options down to the
+	 * current <code>DRDAResultSet</code> object.
+	 *
+	 * @param blksize QRYBLKSZ (Query Block Size)
+	 * @param qryblkctl QRYPRCTYP (Query Protocol Type)
+	 * @param maxblkext MAXBLKEXT (Maximum Number of Extra Blocks)
+	 * @param outovropt OUTOVROPT (Output Override Option)
+	 * @param qryrowset QRYROWSET (Query Rowset Size)
+	 * @param qryclsimpl QRYCLSIMP (Query Close Implicit)
 	 * @see DRDAResultSet#setOPNQRYOptions(int, int, int, int, int, int)
 	 */
 	protected void setOPNQRYOptions(int blksize, int qryblkctl,
 								  int maxblkext, int outovropt,int qryrowset,int qryclsimpl)
 	{
+		this.blksize = blksize;
+		this.qryprctyp = qryblkctl;
+		this.maxblkext = maxblkext;
+		this.outovropt = outovropt;
+		this.qryrowset = qryrowset;
 		currentDrdaRs.setOPNQRYOptions( blksize, qryblkctl, maxblkext, 
 				outovropt, qryrowset, qryclsimpl);
 	}
Index: java/drda/org/apache/derby/impl/drda/DRDAConnThread.java
===================================================================
--- java/drda/org/apache/derby/impl/drda/DRDAConnThread.java	(revisjon 394134)
+++ java/drda/org/apache/derby/impl/drda/DRDAConnThread.java	(arbeidskopi)
@@ -574,6 +574,41 @@
 		}
 	}
 
+    /**
+     * Cleans up and closes a result set if an exception is thrown
+     * when collecting QRYDTA in response to OPNQRY or CNTQRY.
+     *
+     * @param stmt the DRDA statement to clean up
+     * @param sqle the exception that was thrown
+     * @param writerMark start index for the first DSS to clear from
+     * the output buffer
+     * @exception DRDAProtocolException if a DRDA protocol error is
+     * detected
+     */
+    private void cleanUpAndCloseResultSet(DRDAStatement stmt,
+                                          SQLException sqle,
+                                          int writerMark)
+        throws DRDAProtocolException
+    {
+        if (stmt != null) {
+            writer.clearDSSesBackToMark(writerMark);
+            if (!stmt.rsIsClosed()) {
+                try {
+                    stmt.rsClose();
+                } catch (SQLException ec) {
+                    if (SanityManager.DEBUG) {
+                        trace("Warning: Error closing result set");
+                    }
+                }
+                writeABNUOWRM();
+                writeSQLCARD(sqle, CodePoint.SVRCOD_ERROR, 0, 0);
+            }
+        } else {
+            writeSQLCARDs(sqle, 0);
+        }
+        errorInChain(sqle);
+    }
+
 	/**
 	 * Process DRDA commands we can receive once server attributes have been
 	 * exchanged.
@@ -614,30 +649,9 @@
 					}
 					catch(SQLException e)
 					{
-						if (stmt != null)
- 						{
-							// if we got a SQLException we need to clean up and
- 							// close the statement Beetle 4758
-							writer.clearDSSesBackToMark(writerMark);
- 							if (! stmt.rsIsClosed())
- 							{
- 								try {
- 									stmt.rsClose();
- 								}
- 								catch (SQLException ec)
- 								{
- 									if (SanityManager.DEBUG)
- 										trace("Warning: Error closing statement");
-								}
-								writeABNUOWRM();
-								writeSQLCARD(e,CodePoint.SVRCOD_ERROR,0,0);
-							}
-						}
-						else 
-						{
-							writeSQLCARDs(e, 0);
-						}
-						errorInChain(e);
+						// if we got a SQLException we need to clean up and
+						// close the result set Beetle 4758
+						cleanUpAndCloseResultSet(stmt, e, writerMark);
 					}
 					break;
 				case CodePoint.EXCSQLIMM:
@@ -737,11 +751,25 @@
 							checkWarning(null, ps, null, 0, false, true);
 
 							writeQRYDSC(stmt, false);
-							// We could send QRYDTA here if there's no LOB data
-							// in the result set, and if we are using LMTBLKPRC, as
-							// allowed by drda spec, as an option.
 
 							stmt.rsSuspend();
+
+							if (stmt.getQryprctyp() == CodePoint.LMTBLKPRC) {
+								// The DRDA spec allows us to send
+								// QRYDTA here if there are no LOB
+								// columns.
+								DRDAResultSet drdars =
+									stmt.getCurrentDrdaResultSet();
+								try {
+									if (drdars != null &&
+										!drdars.hasLobColumns()) {
+										writeQRYDTA(stmt);
+									}
+								} catch (SQLException sqle) {
+									cleanUpAndCloseResultSet(stmt, sqle,
+															 writerMark);
+								}
+							}
 						}
 					}
 					catch (SQLException e)
Index: java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/values1.inc
===================================================================
--- java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/values1.inc	(revisjon 394134)
+++ java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/values1.inc	(arbeidskopi)
@@ -35,5 +35,12 @@
 endDdm
 endDss
 flush
-skipDss	//ignore OPNQRYRM
-skipDss	//ignore QRYDSC
+readReplyDss
+readLengthAndCodepoint OPNQRYRM
+skipBytes
+readReplyDss
+readLengthAndCodepoint QRYDSC
+skipBytes
+readReplyDss
+readLengthAndCodepoint QRYDTA
+skipBytes
Index: java/testing/org/apache/derbyTesting/functionTests/master/DerbyNet/supersimple.out
===================================================================
--- java/testing/org/apache/derbyTesting/functionTests/master/DerbyNet/supersimple.out	(revisjon 394134)
+++ java/testing/org/apache/derbyTesting/functionTests/master/DerbyNet/supersimple.out	(arbeidskopi)
@@ -149,12 +149,8 @@
 NoHoldForConnection;
 ij(CONNECTION1)> prepare s1 as 'select * from t';
 ij(CONNECTION1)> execute s1;
-I          
------
 ERROR 40XL1: A lock could not be obtained within the time requested
 ij(CONNECTION1)> execute s1;
-I          
------
 ERROR 40XL1: A lock could not be obtained within the time requested
 ij(CONNECTION1)> -- Bug 5967 - Selecting from 2 lob columns w/ the first one having data of length 0
 create table t1 (c1 clob(10), c2 clob(10));
Index: java/testing/org/apache/derbyTesting/functionTests/master/DerbyNetClient/supersimple.out
===================================================================
--- java/testing/org/apache/derbyTesting/functionTests/master/DerbyNetClient/supersimple.out	(revisjon 394134)
+++ java/testing/org/apache/derbyTesting/functionTests/master/DerbyNetClient/supersimple.out	(arbeidskopi)
@@ -149,12 +149,8 @@
 NoHoldForConnection;
 ij(CONNECTION1)> prepare s1 as 'select * from t';
 ij(CONNECTION1)> execute s1;
-I          
------
 ERROR 40XL1: A lock could not be obtained within the time requested
 ij(CONNECTION1)> execute s1;
-I          
------
 ERROR 40XL1: A lock could not be obtained within the time requested
 ij(CONNECTION1)> -- Bug 5967 - Selecting from 2 lob columns w/ the first one having data of length 0
 create table t1 (c1 clob(10), c2 clob(10));
Index: java/testing/org/apache/derbyTesting/functionTests/master/DerbyNetClient/forupdate.out
===================================================================
--- java/testing/org/apache/derbyTesting/functionTests/master/DerbyNetClient/forupdate.out	(revisjon 394134)
+++ java/testing/org/apache/derbyTesting/functionTests/master/DerbyNetClient/forupdate.out	(arbeidskopi)
@@ -43,7 +43,7 @@
 ----- with a 'cursor not updatable' message
 get cursor c as 'select i, v from t1, t2';
 ij> delete from t1 where current of c;
-ERROR 42X23: Cursor SQL_CURLH000C1 is not updatable.
+ERROR 42X30: Cursor 'SQL_CURLH000C1' not found. Verify that autocommit is OFF.
 ij> --  cursor with same name already exists
 get cursor c as 'select i, v from t1, t2';
 ERROR (no SQLState): Duplicate cursor names are not allowed.
@@ -62,13 +62,13 @@
 ----- we know because the delete is refused with a 'cursor not updatable' message
 get cursor c2 as 'select i, v from t1, t2 for read only';
 ij> delete from t1 where current of c2;
-ERROR 42X23: Cursor SQL_CURLH000C1 is not updatable.
+ERROR 42X30: Cursor 'SQL_CURLH000C1' not found. Verify that autocommit is OFF.
 ij> close c2;
 ij> -- . read only for updatable cursor spec
 ----- we know because the delete is refused with a 'cursor not updatable' message
 get cursor c3 as 'select i, v from t1 where i is not null for read only';
 ij> delete from t1 where current of c3;
-ERROR 42X23: Cursor SQL_CURLH000C1 is not updatable.
+ERROR 42X30: Cursor 'SQL_CURLH000C1' not found. Verify that autocommit is OFF.
 ij> close c3;
 ij> -- . for update col not in select list
 ----- this is allowed:
