Index: java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java
===================================================================
--- java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java	(revision 410866)
+++ java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java	(working copy)
@@ -487,16 +487,9 @@
 			//if (onRow && !(currentRow instanceof org.apache.derby.impl.sql.execute.ValueRow))
 			//	System.out.println(currentRow.getClass());
 
-		    /*
-			    Connection.setAutoCommit says that a statement completes,
-			    and will autoCommit, when it fetches the last row or is closed.
-			    This means a close will get a "Cursor already closed" error.
-				This rule only applies when doing a next() - if it were applied
-				to scrolling actions (like FIRST or LAST) it would close
-				the cursor when doing any operation on a scrolling cursor.
-
-			    if autocommit, this will commit
-		     */
+		    // The ResultSet may implicitly close when when the ResultSet type 
+		    // is TYPE_FORWARD_ONLY and the next method of ResultSet returns 
+		    // false. This will cause a commit if autocommit = true.
 		    if (!onRow && (position == NEXT)) {
 
 		     // In case of resultset for MetaData, we will only commit
@@ -515,8 +508,10 @@
 		     	// we do not want to commit here as there seems to be other
 		     	// statements/resultSets currently opened for this connection.
 		     } else if (owningStmt != null)
-				 // allow the satement to commit if required.
-		     	owningStmt.resultSetClosing(this);
+		         if (owningStmt.getResultSetType() == TYPE_FORWARD_ONLY) {
+		            // allow the satement to commit if required.
+		            owningStmt.resultSetClosing(this);
+		         }
 		    }
 
 			// Clear the indication of which columns were fetched as streams.
Index: java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/build.xml
===================================================================
--- java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/build.xml	(revision 410866)
+++ java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/build.xml	(working copy)
@@ -82,6 +82,7 @@
       <exclude name="${this.dir}/xaJNDI.java"/>
       <exclude name="${this.dir}/lobStreams.java"/>
       <exclude name="${this.dir}/XATest.java"/>
+      <exclude name="${this.dir}/ScrollResultSetTest.java"/>
     </javac>
   </target>
 
@@ -101,6 +102,7 @@
       destdir="${out.dir}">
       <classpath>
         <pathelement path="${java14compile.classpath}"/>
+        <pathelement path="${junit}"/>
       </classpath>
       <!--exclude name=""/-->
       <include name="${this.dir}/autoGeneratedJdbc30.java"/>
@@ -113,6 +115,7 @@
       <include name="${this.dir}/statementJdbc30.java"/>
       <include name="${this.dir}/lobStreams.java"/>
       <include name="${this.dir}/XATest.java"/>
+      <include name="${this.dir}/ScrollResultSetTest.java"/>
     </javac>
   </target>
 
Index: java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ScrollResultSetTest.java
===================================================================
--- java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ScrollResultSetTest.java	(revision 0)
+++ java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ScrollResultSetTest.java	(revision 0)
@@ -0,0 +1,144 @@
+/*
+ *
+ * Derby - Class ScrollResultSetTest
+ *
+ * Copyright 2006 The Apache Software Foundation or its 
+ * licensors, as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, 
+ * software distributed under the License is distributed on an 
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
+ * either express or implied. See the License for the specific 
+ * language governing permissions and limitations under the License.
+ */
+package org.apache.derbyTesting.functionTests.tests.jdbcapi;
+
+import org.apache.derbyTesting.functionTests.util.TestUtil;
+import org.apache.derbyTesting.functionTests.util.BaseJDBCTestCase;
+import junit.framework.*;
+import java.sql.*;
+
+/**
+ * Tests scrollable result sets
+ *
+ * @author Fernanda Pizzorno
+ *
+ * Tests:
+ * - testNextOnLastRowForwardOnly: tests that the result set is closed when all
+ * rows have been retreived and next has been called from the last row, 
+ * autocommit = true, the result set is not holdable and type forward 
+ * only. (DERBY-1295)
+ * - testNextOnLastRowScrollable: tests that the result set is not closed when 
+ * next is called while the result set is positioned in the last row, 
+ * autocommit = true, the result set is not holdable type scrollable 
+ * insensitive. (DERBY-1295)
+ *
+ */
+public class ScrollResultSetTest extends BaseJDBCTestCase {
+    
+    /** Creates a new instance of SURBaseTest */
+    public ScrollResultSetTest(String name) {
+        super(name);
+    }
+
+    /**
+     * Set up the connection to the database.
+     */
+    public void setUp() throws  Exception {       
+        con = getConnection();
+        con.setAutoCommit(true);
+
+        String createTableWithPK = "CREATE TABLE tableWithPK (" +
+                "c1 int primary key," +
+                "c2 int)";
+        String insertData = "INSERT INTO tableWithPK values " +
+                "(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)";
+        Statement stmt = con.createStatement();
+        stmt.execute(createTableWithPK);
+        
+        stmt.execute(insertData);
+    }
+    
+    /**
+     * Rollback the transaction
+     */
+    public void tearDown() throws Exception {
+        println("TearDown");
+        try { 
+            con.createStatement().executeUpdate("DROP TABLE tableWithPK");
+            con.commit();
+            con.close();
+        } catch (SQLException e) {
+            printStackTrace(e);
+        }      
+    }
+    
+    /**
+     * Test that moving to next row after positioned at the last row on a 
+     * forward only result set will close the result set
+     */
+    public void testNextOnLastRowForwardOnly()  throws SQLException{
+
+        con.setAutoCommit(true);
+        con.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
+        Statement roStmt = con.createStatement(
+                ResultSet.TYPE_FORWARD_ONLY, 
+                ResultSet.CONCUR_READ_ONLY);
+
+        ResultSet rs = roStmt.executeQuery("SELECT c1 FROM tableWithPK");
+
+        // call next until positioned after last
+        boolean b = true;
+        while (b = rs.next());
+        assertFalse("Calling next while positioned after last returns " +
+                "false", b);
+        
+        try {
+            // forward only result set should be closed now, an exception will
+            // be thrown
+            b = rs.next();
+            assertTrue("Should not get here!", false);
+        } catch (SQLException se) {
+            assertEquals(se.getSQLState(), RESULT_SET_IS_CLOSED);
+        }
+
+    }
+
+    /**
+     * Test that moving to next row after positioned at the last row on a 
+     * scrollable result set will not close the result set
+     */
+    public void testNextOnLastRowScrollable()  throws SQLException{
+
+        con.setAutoCommit(true);
+        con.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
+        Statement roStmt = con.createStatement(
+                ResultSet.TYPE_SCROLL_INSENSITIVE, 
+                ResultSet.CONCUR_READ_ONLY);
+
+        ResultSet rs = roStmt.executeQuery("SELECT c1 FROM tableWithPK");
+        // move to last position and then call next
+        rs.last();
+        rs.next();
+        
+        // scrollable result set should still be open and not throw no 
+        // exception will be thrown
+        assertFalse("Calling next while positioned after last returns " +
+                "false", rs.next());
+        assertTrue("Moving to absolute(2) returns true", rs.absolute(2));
+        rs.close();
+
+    }
+
+
+    protected Connection con = null; // Connection established in setUp()
+    
+    final static String RESULT_SET_IS_CLOSED = "XCL16";
+       
+}

Property changes on: java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ScrollResultSetTest.java
___________________________________________________________________
Name: svn:eol-style
   + native

Index: java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ScrollResultSetTest_app.properties
===================================================================
--- java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ScrollResultSetTest_app.properties	(revision 0)
+++ java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ScrollResultSetTest_app.properties	(revision 0)
@@ -0,0 +1,3 @@
+runwithibm13=false
+runwithjdk13=false
+runwithjdk12=false

Property changes on: java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ScrollResultSetTest_app.properties
___________________________________________________________________
Name: svn:eol-style
   + native

Index: java/testing/org/apache/derbyTesting/functionTests/suites/jdbcapi.runall
===================================================================
--- java/testing/org/apache/derbyTesting/functionTests/suites/jdbcapi.runall	(revision 410866)
+++ java/testing/org/apache/derbyTesting/functionTests/suites/jdbcapi.runall	(working copy)
@@ -24,4 +24,5 @@
 jdbcapi/UpdateXXXTest.junit
 jdbcapi/SURQueryMixTest.junit
 jdbcapi/SURTest.junit
+jdbcapi/ScrollResultSetTest.junit
 jdbcapi/URCoveringIndexTest.junit
Index: java/testing/org/apache/derbyTesting/functionTests/suites/DerbyNet.exclude
===================================================================
--- java/testing/org/apache/derbyTesting/functionTests/suites/DerbyNet.exclude	(revision 410866)
+++ java/testing/org/apache/derbyTesting/functionTests/suites/DerbyNet.exclude	(working copy)
@@ -43,6 +43,7 @@
 jdbcapi/SURTest.junit
 jdbcapi/ConcurrencyTest.junit
 jdbcapi/HoldabilityTest.junit
+jdbcapi/ScrollResultSetTest.junit
 # Excluding checkDataSource and checkDataSource30 because JCC has no XA
 jdbcapi/checkDataSource30.java
 jdbcapi/checkDataSource.java
