KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3 Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.derbyStress
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 java.sql.Connection JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import org.apache.derby.tools.ij;
31 import org.apache.derbyTesting.functionTests.util.TestUtil;
32
33 public class derbyStress {
34     
35     private static String JavaDoc[] testObjects = {"table t1"};
36     
37     private static int numConn = 1;
38     private static int numRows = 100;
39     private static int numPreparedStmts = 2000;
40
41     public static void main(String JavaDoc[] args) {
42         try {
43             System.out.println("Test derbyStress starting");
44
45             // use the ij utility to read the property file and
46
// make the initial connection.
47
ij.getPropertyArg(args);
48             Connection JavaDoc conn = null;
49             
50             for (int i = 0; i < numConn; i++) {
51                  conn = ij.startJBMS();
52                  System.out.println("Testing with " + numPreparedStmts + " prepared statements");
53                  prepStmtTest(conn, numRows, numPreparedStmts);
54                  System.out.println("PASS -- Prepared statement test");
55                  conn.close();
56             }
57
58             reExecuteStatementTest();
59
60             System.out.println("Test derbyStress finished.");
61         } catch (SQLException JavaDoc se) {
62             TestUtil.dumpSQLExceptions(se);
63         } catch (Throwable JavaDoc e) {
64             System.out.println("FAIL -- unexpected exception caught in main():\n");
65             System.out.println(e.getMessage());
66             e.printStackTrace();
67         }
68     }
69     
70     private static void createTables(Connection JavaDoc conn, int numRows) throws SQLException JavaDoc{
71         Statement JavaDoc stmt = conn.createStatement();
72         
73         TestUtil.cleanUpTest(stmt, testObjects);
74         
75         stmt.execute("create table t1 (lvc LONG VARCHAR)");
76         stmt.close();
77         
78         String JavaDoc insertTabSql = "insert into t1 values(?)";
79         PreparedStatement JavaDoc ps = conn.prepareStatement(insertTabSql);
80          for (int i = 0; i < numRows; i++)
81          {
82              ps.setString(1,"Hello" + i);
83              ps.executeUpdate();
84          }
85          ps.close();
86     }
87     
88     // Tests prepared statements are not leaked if not explicitly closed by
89
// user (DERBY-210)
90
private static void prepStmtTest(Connection JavaDoc conn, int numRows, int numPreparedStmts) throws Exception JavaDoc
91     {
92         PreparedStatement JavaDoc ps = null;
93         ResultSet JavaDoc rs = null;
94         conn.setAutoCommit(false);
95          
96         try {
97         
98             createTables(conn, numRows);
99             
100             String JavaDoc selTabSql = "select * from t1";
101             
102             for (int i = 0 ; i < numPreparedStmts; i++)
103             {
104                 ps = conn.prepareStatement(selTabSql);
105                 rs = ps.executeQuery();
106
107                 while (rs.next())
108                 {
109                     rs.getString(1);
110                 }
111
112                 rs.close();
113             
114                 // Do not close the prepared statement
115
//ps.close();
116
}
117             conn.commit();
118         }
119         catch (SQLException JavaDoc e) {
120             TestUtil.dumpSQLExceptions(e);
121             conn.rollback();
122         }
123     }
124
125     // Tests re-execution of a statement without closing the result
126
// set (DERBY-557).
127
private static void reExecuteStatementTest() throws Exception JavaDoc {
128         System.out.print("DERBY-557: reExecuteStatementTest() ");
129         Connection JavaDoc conn = ij.startJBMS();
130         Statement JavaDoc stmt = conn.createStatement();
131         for (int i = 0; i < 50000; i++) {
132             ResultSet JavaDoc rs = stmt.executeQuery("values(1)");
133             // How silly! I forgot to close the result set.
134
}
135         TestUtil.cleanUpTest(stmt, testObjects);
136         conn.commit();
137         stmt.close();
138         conn.close();
139         System.out.println("PASSED");
140     }
141 }
142
Popular Tags