File: ResultSetCloseTest.java

package info (click to toggle)
derby 10.14.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 78,896 kB
  • sloc: java: 691,930; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 60
file content (126 lines) | stat: -rw-r--r-- 3,937 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 
   Derby - Class ResultSetCloseTest
 
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You 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 junit.framework.*;

import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.TestConfiguration;

import java.sql.*;

/**
 * This class is used to test the fix for DERBY-694.
 *
 * A brief description of DERBY-694 (Got from the description in JIRA)
 *
 * 1) Autocommit off.
 * 2) Have two prepared statements, calling executeQuery() on both
 * 3) Gives two result sets. Can fetch data from both with next().
 * 4) If one statement gets an exception (say, caused by a division by zero)
 * 5) not only this statement's result set is closed, but also the other open
 *    resultset. This happens with the client driver, whereas in embedded mode,
 *    the other result set is unaffected by the exception in the first result set
 *    (as it should be).
 *
 */
public class ResultSetCloseTest extends BaseJDBCTestCase {
  
   
    /**
     * Create the tables and the Connection and PreparedStatements that will
     * be used in this test.
     */
    public void setUp()
    throws SQLException {
        Connection con = getConnection();
        con.setAutoCommit(false);
        
        Statement s = con.createStatement();
        
        s.execute("create table t1 (a int)");
        
        s.execute("insert into t1 values(1)");
        s.execute("insert into t1 values(0)");
        s.execute("insert into t1 values(2)");
        s.execute("insert into t1 values(3)");
        
        s.close();
        
        con.commit();
    }
    
    /**
     * Test that the occurence of the exception in one of the PreparedStatements
     * does not result in the closure of the ResultSet associated with the other
     * Prepared Statements.
     *
     * STEPS :
     * 1) Execute the first PreparedStatement. This should not cause any
     *    SQLException.
     * 2) Now execute the second PreparedStatement. This causes
     *    the expected Divide by zero exception.
     * 3) Now access the first resultset again to ensure this is still open.
     *
     */
    public void testResultSetDoesNotClose() throws SQLException {
        
        PreparedStatement ps1 = prepareStatement("select * from t1");
        PreparedStatement ps2 = prepareStatement("select 10/a from t1");
        
        ResultSet rs1 = ps1.executeQuery();
        
        try {
            ResultSet rs2 = ps2.executeQuery();
            while(rs2.next());
        } catch(SQLException sqle) {
            //Do Nothing expected exception
        }
        
        while(rs1.next());
        
        commit();
        
        rs1.close();
        ps1.close();
        ps2.close();
    }
    
    /**
     * Create the test with the given name.
     *
     * @param name name of the test.
     */
    public ResultSetCloseTest(String name) {
        super(name);
    }
    
    /**
     * Create test suite for this test.
     * Run in both embedded and client.
     */
    public static Test suite() {
                
        return TestConfiguration.defaultSuite(ResultSetCloseTest.class);
    }
    
}