KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.holdCursorJavaReflection
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.lang.reflect.*;
25
26 import java.sql.CallableStatement JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.ResultSetMetaData JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.sql.Statement JavaDoc;
33 import java.sql.Types JavaDoc;
34
35 import org.apache.derby.tools.ij;
36 import org.apache.derby.tools.JDBCDisplayUtil;
37
38 /**
39  * Test hold cursor after commit using reflection. This test is specifically to test
40  * this feature under jdk13.
41  */

42 public class holdCursorJavaReflection {
43
44   //we implemented hold cursor functionality in EmbedConnection20 package and hence
45
//the functionality is available under both jdk14 and jdk13 (though, jdbc in jdk13
46
//doesn't have the api to access it).
47
//An internal project in TIVOLI needed access to holdability under jdk13 and we
48
//recommended them to use reflection to get to holdability apis under jdk13.
49
//This will also be documented on our website under faq
50
//In order to have a test for that workaround in our test suite, I am using reflection
51
//for createStatement and prepareStatement and prepareCall.
52

53   //prepareStatement and prepareCall take 4 parameters
54
private static Class JavaDoc[] PREP_STMT_PARAM = { String JavaDoc.class, Integer.TYPE, Integer.TYPE, Integer.TYPE };
55   private static Object JavaDoc[] PREP_STMT_ARG = { "select * from t1 where c12 = ?", new Integer JavaDoc(ResultSet.TYPE_FORWARD_ONLY),
56    new Integer JavaDoc(ResultSet.CONCUR_READ_ONLY), new Integer JavaDoc(ResultSet.HOLD_CURSORS_OVER_COMMIT)};
57
58   private static Object JavaDoc[] PREP_STMT_ERROR_ARG = { "select * from t1NotThere where c12 = ?",
59                                             new
60                                                 Integer JavaDoc(ResultSet.TYPE_FORWARD_ONLY), new Integer JavaDoc(ResultSet.CONCUR_READ_ONLY), new Integer JavaDoc(ResultSet.HOLD_CURSORS_OVER_COMMIT)};
61     
62   //createStatement takes 3 parameters
63
private static Class JavaDoc[] STMT_PARAM = { Integer.TYPE, Integer.TYPE, Integer.TYPE };
64   private static Object JavaDoc[] STMT_ARG = { new Integer JavaDoc(ResultSet.TYPE_FORWARD_ONLY),
65    new Integer JavaDoc(ResultSet.CONCUR_READ_ONLY), new Integer JavaDoc(ResultSet.HOLD_CURSORS_OVER_COMMIT)};
66
67   public static void main (String JavaDoc args[])
68   {
69     try {
70         /* Load the JDBC Driver class */
71         // use the ij utility to read the property file and
72
// make the initial connection.
73
ij.getPropertyArg(args);
74         Connection JavaDoc conn = ij.startJBMS();
75
76         dropTable(conn);
77         createAndPopulateTable(conn);
78
79     //set autocommit to off after creating table and inserting data
80
conn.setAutoCommit(false);
81         testHoldability(conn);
82         testPreparedStatement(conn);
83         testCallableStatement(conn);
84         conn.rollback();
85         conn.setAutoCommit(true);
86         dropTable(conn);
87         conn.close();
88     } catch (Exception JavaDoc e) {
89         System.out.println("FAIL -- unexpected exception "+e);
90         JDBCDisplayUtil.ShowException(System.out, e);
91         e.printStackTrace();
92     }
93   }
94
95   //create table and insert couple of rows
96
private static void createAndPopulateTable(Connection JavaDoc conn) throws SQLException JavaDoc {
97     Statement JavaDoc stmt = conn.createStatement();
98
99     System.out.println("Creating table...");
100     stmt.executeUpdate( "CREATE TABLE T1 (c11 int, c12 int)" );
101     stmt.executeUpdate("INSERT INTO T1 VALUES(1,1)");
102     stmt.executeUpdate("INSERT INTO T1 VALUES(2,1)");
103     System.out.println("done creating table and inserting data.");
104
105     stmt.close();
106   }
107
108   //drop table
109
private static void dropTable(Connection JavaDoc conn)
110 // throws SQLException
111
{
112     try {
113         Statement JavaDoc stmt = conn.createStatement();
114         stmt.executeUpdate( "DROP TABLE T1" );
115         stmt.close();
116     } catch (SQLException JavaDoc se) {} // assume any error is because table doesn't exist
117
}
118
119   //test cursor holdability for callable statements
120
private static void testCallableStatement(Connection JavaDoc conn) throws Exception JavaDoc
121   {
122     CallableStatement JavaDoc cs;
123     ResultSet JavaDoc rs;
124
125     System.out.println("Start hold cursor for callable statements test");
126
127     //create a callable statement with hold cursor over commit using reflection.
128
Method sh = conn.getClass().getMethod("prepareCall", PREP_STMT_PARAM);
129     cs = (CallableStatement JavaDoc) (sh.invoke(conn, PREP_STMT_ARG));
130     cs.setInt(1,1);
131     rs = cs.executeQuery();
132
133     System.out.println("do next() before commit");
134     rs.next();
135     System.out.println("look at first column's value: " + rs.getInt(1));
136     conn.commit();
137     System.out.println("After commit, look at first column's value: " + rs.getInt(1));
138     System.out.println("do next() after commit. Should be at the end of resultset");
139     rs.next();
140     System.out.println("one more next() here will give no more rows");
141     rs.next();
142     System.out.println("Holdable cursor after commit for callable statements test over");
143     rs.close();
144   }
145
146   //test cursor holdability after commit
147
private static void testHoldability(Connection JavaDoc conn) throws Exception JavaDoc
148   {
149     Statement JavaDoc s;
150     PreparedStatement JavaDoc ps;
151     ResultSet JavaDoc rs;
152
153     System.out.println("Start holdable cursor after commit test");
154     //create a statement with hold cursor over commit using reflection.
155
Method sh = conn.getClass().getMethod("createStatement", STMT_PARAM);
156     s = (Statement JavaDoc) (sh.invoke(conn, STMT_ARG));
157
158     //open a cursor with multiple rows resultset
159
rs = s.executeQuery("select * from t1");
160     System.out.println("do next() before commit");
161     rs.next();
162     System.out.println("look at first column's value: " + rs.getInt(1));
163     conn.commit();
164     System.out.println("After commit, look at first column's value: " + rs.getInt(1));
165     System.out.println("do next() after commit. Should be at the end of resultset");
166     rs.next();
167     System.out.println("one more next() here will give no more rows");
168     rs.next();
169     System.out.println("Holdable cursor after commit test over");
170     rs.close();
171   }
172
173   //test cursor holdability for prepared statements
174
private static void testPreparedStatement(Connection JavaDoc conn) throws Exception JavaDoc
175   {
176     PreparedStatement JavaDoc ps;
177     ResultSet JavaDoc rs;
178
179     System.out.println("Start hold cursor for prepared statements test");
180
181     //create a prepared statement with hold cursor over commit using reflection.
182
Method sh = conn.getClass().getMethod("prepareStatement", PREP_STMT_PARAM);
183     ps = (PreparedStatement JavaDoc) (sh.invoke(conn, PREP_STMT_ARG));
184
185     ps.setInt(1,1);
186     rs = ps.executeQuery();
187
188     System.out.println("do next() before commit");
189     rs.next();
190     System.out.println("look at first column's value: " + rs.getInt(1));
191     conn.commit();
192     System.out.println("After commit, look at first column's value: " + rs.getInt(1));
193     System.out.println("do next() after commit. Should be at the end of resultset");
194     rs.next();
195     System.out.println("one more next() here will give no more rows");
196     rs.next();
197     System.out.println("Holdable cursor after commit for prepared statements test over");
198     rs.close();
199
200     // Create a prepared statement that will fail on prepare. Make sure we
201
// handle errors ok.
202
sh = conn.getClass().getMethod("prepareStatement", PREP_STMT_PARAM);
203     try {
204         ps = (PreparedStatement JavaDoc) (sh.invoke(conn, PREP_STMT_ERROR_ARG));
205         
206         ps.setInt(1,1);
207         rs = ps.executeQuery();
208     }
209     catch (SQLException JavaDoc se)
210     {
211         System.out.println("Expected Exception:" + se.getMessage());
212     }
213     catch (InvocationTargetException itex) {
214         Throwable JavaDoc e = itex.getTargetException();
215         //prepareStatement Can only throw SQLExcepton
216
if (e instanceof SQLException JavaDoc)
217         {
218             SQLException JavaDoc se = (SQLException JavaDoc)e;
219             System.out.println("Expected Exception:" + se.getMessage());
220         }
221         else
222             throw itex;
223     }
224     // make sure our connection is still ok.
225
conn.commit();
226   }
227     
228 }
229
230
231
Popular Tags