001: /*
002:
003: Derby - Class ResultSetCloseTest
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.jdbcapi;
023:
024: import junit.framework.*;
025:
026: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
027:
028: import java.sql.*;
029:
030: /**
031: * This class is used to test the fix for DERBY-694.
032: *
033: * A brief description of DERBY-694 (Got from the description in JIRA)
034: *
035: * 1) Autocommit off.
036: * 2) Have two prepared statements, calling executeQuery() on both
037: * 3) Gives two result sets. Can fetch data from both with next().
038: * 4) If one statement gets an exception (say, caused by a division by zero)
039: * 5) not only this statement's result set is closed, but also the other open
040: * resultset. This happens with the client driver, whereas in embedded mode,
041: * the other result set is unaffected by the exception in the first result set
042: * (as it should be).
043: *
044: */
045: public class ResultSetCloseTest extends BaseJDBCTestCase {
046:
047: /**
048: * Create the tables and the Connection and PreparedStatements that will
049: * be used in this test.
050: */
051: public void setUp() throws SQLException {
052: Connection con = getConnection();
053: con.setAutoCommit(false);
054:
055: Statement s = con.createStatement();
056:
057: s.execute("create table t1 (a int)");
058:
059: s.execute("insert into t1 values(1)");
060: s.execute("insert into t1 values(0)");
061: s.execute("insert into t1 values(2)");
062: s.execute("insert into t1 values(3)");
063:
064: s.close();
065:
066: con.commit();
067: }
068:
069: /**
070: * Test that the occurence of the exception in one of the PreparedStatements
071: * does not result in the closure of the ResultSet associated with the other
072: * Prepared Statements.
073: *
074: * STEPS :
075: * 1) Execute the first PreparedStatement. This should not cause any
076: * SQLException.
077: * 2) Now execute the second PreparedStatement. This causes
078: * the expected Divide by zero exception.
079: * 3) Now access the first resultset again to ensure this is still open.
080: *
081: */
082: public void testResultSetDoesNotClose() throws SQLException {
083:
084: PreparedStatement ps1 = prepareStatement("select * from t1");
085: PreparedStatement ps2 = prepareStatement("select 10/a from t1");
086:
087: ResultSet rs1 = ps1.executeQuery();
088:
089: try {
090: ResultSet rs2 = ps2.executeQuery();
091: while (rs2.next())
092: ;
093: } catch (SQLException sqle) {
094: //Do Nothing expected exception
095: }
096:
097: while (rs1.next())
098: ;
099:
100: commit();
101:
102: rs1.close();
103: ps1.close();
104: ps2.close();
105: }
106:
107: /**
108: * Create the test with the given name.
109: *
110: * @param name name of the test.
111: */
112: public ResultSetCloseTest(String name) {
113: super (name);
114: }
115:
116: /**
117: * Create test suite for this test.
118: */
119: public static Test suite() {
120:
121: TestSuite suite = new TestSuite("ResultSetCloseTest");
122:
123: // DB2 client doesn't implement result set closing
124: // correctly wrt ensuring all its methods subsequently
125: // throw an exception.
126: if (usingDerbyNet())
127: return suite;
128:
129: suite.addTestSuite(ResultSetCloseTest.class);
130: return suite;
131: }
132:
133: }
|