KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > bug5052rts


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.bug5052rts
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.lang;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Types JavaDoc;
31 import java.sql.*;
32
33 import org.apache.derby.tools.ij;
34 import org.apache.derby.tools.JDBCDisplayUtil;
35
36
37 /**
38  * testing gathering of runtime statistics for
39  * for the resultsets/statements not closed by the usee,
40  * but get closed when garbage collector collects such
41  * objects and closes them by calling the finalize.
42  * See bug : 5052 for details.
43  *
44  */

45
46 public class bug5052rts {
47  
48     public static void main(String JavaDoc[] args) {
49         Connection JavaDoc conn;
50         Statement JavaDoc stmt;
51         PreparedStatement pstmt;
52
53         System.out.println("Test RunTime Statistics starting");
54         try
55         {
56             // use the ij utility to read the property file and
57
// make the initial connection.
58
ij.getPropertyArg(args);
59             conn = ij.startJBMS();
60             stmt = conn.createStatement();
61             try{
62                stmt.execute("drop table tab1");
63             }catch(Throwable JavaDoc e ) {}
64
65             //create a table, insert a row, do a select from the table,
66
stmt.execute("create table tab1 (COL1 int, COL2 smallint, COL3 real)");
67             stmt.executeUpdate("insert into tab1 values(1, 2, 3.1)");
68             stmt.executeUpdate("insert into tab1 values(2, 2, 3.1)");
69             conn.setAutoCommit( false );
70
71             //case1: Setting runtime statistics on just before result set close.
72
if(true)
73             {
74                 Statement JavaDoc stmt0 = conn.createStatement();
75                 ResultSet JavaDoc rs = stmt0.executeQuery("select * from tab1"); // opens the result set
76

77                 while ( rs.next() ) {
78                     System.out.println(rs.getString(1));
79                 }
80                 //set the runtime statistics on now.
81
CallableStatement cs =
82                     conn.prepareCall(
83                         "CALL SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(?)");
84                 cs.setInt(1, 1);
85                 cs.execute();
86                 cs.close();
87
88                 rs.close();
89                 stmt0.close();
90             }
91             
92             CallableStatement cs =
93                 conn.prepareCall(
94                     "CALL SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(?)");
95             cs.setInt(1, 0);
96             cs.execute();
97             cs.close();
98             
99             //case2: Stament/Resutset getting closed by the Garbage collector.
100
if(true)
101             {
102                 Statement JavaDoc stmt1 = conn.createStatement();
103                 ResultSet JavaDoc rs = stmt1.executeQuery("select * from tab1"); // opens the result set
104

105                 while ( rs.next() ) {
106                     System.out.println(rs.getString(1));
107                 }
108                 //set the runtime statistics on now.
109
cs = conn.prepareCall(
110                         "CALL SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(?)");
111                 cs.setInt(1, 1);
112                 cs.execute();
113                 cs.close();
114             }
115
116             
117             if(true)
118             {
119                 Statement JavaDoc stmt2 = conn.createStatement();
120                 ResultSet JavaDoc rs1 = stmt2.executeQuery("select count(*) from tab1"); // opens the result set
121

122                 while ( rs1.next() ) {
123                     System.out.println(rs1.getString(1));
124                 }
125             }
126
127             
128             for(int i = 0 ; i < 3 ; i++){
129                 System.gc();
130                 System.runFinalization();
131                 //sleep for sometime to make sure garbage collector kicked in
132
//and collected the result set object.
133
Thread.sleep(3000);
134             }
135
136             conn.commit(); //This should have failed before we fix 5052
137
conn.close();
138         }
139         
140         catch (SQLException JavaDoc e) {
141             dumpSQLExceptions(e);
142             e.printStackTrace();
143         }
144         catch (Throwable JavaDoc e) {
145             System.out.println("FAIL -- unexpected exception: "+e);
146             e.printStackTrace();
147         }
148         System.out.println("Test RunTimeStatistics finished successfully");
149     }
150
151     static private void dumpSQLExceptions (SQLException JavaDoc se) {
152         System.out.println("FAIL -- unexpected exception");
153         while (se != null) {
154             System.out.println("SQLSTATE("+se.getSQLState()+"): "+se);
155             se = se.getNextException();
156         }
157     }
158
159 }
160
161
162
163
164
165
166
167
Popular Tags