KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > ResultSetCloseTest


1 /*
2  
3    Derby - Class ResultSetCloseTest
4  
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13  
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19  
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
23
24 import junit.framework.*;
25
26 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
27
28 import java.sql.*;
29
30 /**
31  * This class is used to test the fix for DERBY-694.
32  *
33  * A brief description of DERBY-694 (Got from the description in JIRA)
34  *
35  * 1) Autocommit off.
36  * 2) Have two prepared statements, calling executeQuery() on both
37  * 3) Gives two result sets. Can fetch data from both with next().
38  * 4) If one statement gets an exception (say, caused by a division by zero)
39  * 5) not only this statement's result set is closed, but also the other open
40  * resultset. This happens with the client driver, whereas in embedded mode,
41  * the other result set is unaffected by the exception in the first result set
42  * (as it should be).
43  *
44  */

45 public class ResultSetCloseTest extends BaseJDBCTestCase {
46   
47    
48     /**
49      * Create the tables and the Connection and PreparedStatements that will
50      * be used in this test.
51      */

52     public void setUp()
53     throws SQLException {
54         Connection con = getConnection();
55         con.setAutoCommit(false);
56         
57         Statement s = con.createStatement();
58         
59         s.execute("create table t1 (a int)");
60         
61         s.execute("insert into t1 values(1)");
62         s.execute("insert into t1 values(0)");
63         s.execute("insert into t1 values(2)");
64         s.execute("insert into t1 values(3)");
65         
66         s.close();
67         
68         con.commit();
69     }
70     
71     /**
72      * Test that the occurence of the exception in one of the PreparedStatements
73      * does not result in the closure of the ResultSet associated with the other
74      * Prepared Statements.
75      *
76      * STEPS :
77      * 1) Execute the first PreparedStatement. This should not cause any
78      * SQLException.
79      * 2) Now execute the second PreparedStatement. This causes
80      * the expected Divide by zero exception.
81      * 3) Now access the first resultset again to ensure this is still open.
82      *
83      */

84     public void testResultSetDoesNotClose() throws SQLException {
85         
86         PreparedStatement ps1 = prepareStatement("select * from t1");
87         PreparedStatement ps2 = prepareStatement("select 10/a from t1");
88         
89         ResultSet rs1 = ps1.executeQuery();
90         
91         try {
92             ResultSet rs2 = ps2.executeQuery();
93             while(rs2.next());
94         } catch(SQLException sqle) {
95             //Do Nothing expected exception
96
}
97         
98         while(rs1.next());
99         
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 JavaDoc 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 }
134
Popular Tags