001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.derbyStress
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.jdbcapi;
023:
024: import java.sql.Connection;
025: import java.sql.Statement;
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028: import java.sql.SQLException;
029:
030: import org.apache.derby.tools.ij;
031: import org.apache.derbyTesting.functionTests.util.TestUtil;
032:
033: public class derbyStress {
034:
035: private static String[] testObjects = { "table t1" };
036:
037: private static int numConn = 1;
038: private static int numRows = 100;
039: private static int numPreparedStmts = 2000;
040:
041: public static void main(String[] args) {
042: try {
043: System.out.println("Test derbyStress starting");
044:
045: // use the ij utility to read the property file and
046: // make the initial connection.
047: ij.getPropertyArg(args);
048: Connection conn = null;
049:
050: for (int i = 0; i < numConn; i++) {
051: conn = ij.startJBMS();
052: System.out.println("Testing with " + numPreparedStmts
053: + " prepared statements");
054: prepStmtTest(conn, numRows, numPreparedStmts);
055: System.out.println("PASS -- Prepared statement test");
056: conn.close();
057: }
058:
059: reExecuteStatementTest();
060:
061: System.out.println("Test derbyStress finished.");
062: } catch (SQLException se) {
063: TestUtil.dumpSQLExceptions(se);
064: } catch (Throwable e) {
065: System.out
066: .println("FAIL -- unexpected exception caught in main():\n");
067: System.out.println(e.getMessage());
068: e.printStackTrace();
069: }
070: }
071:
072: private static void createTables(Connection conn, int numRows)
073: throws SQLException {
074: Statement stmt = conn.createStatement();
075:
076: TestUtil.cleanUpTest(stmt, testObjects);
077:
078: stmt.execute("create table t1 (lvc LONG VARCHAR)");
079: stmt.close();
080:
081: String insertTabSql = "insert into t1 values(?)";
082: PreparedStatement ps = conn.prepareStatement(insertTabSql);
083: for (int i = 0; i < numRows; i++) {
084: ps.setString(1, "Hello" + i);
085: ps.executeUpdate();
086: }
087: ps.close();
088: }
089:
090: // Tests prepared statements are not leaked if not explicitly closed by
091: // user (DERBY-210)
092: private static void prepStmtTest(Connection conn, int numRows,
093: int numPreparedStmts) throws Exception {
094: PreparedStatement ps = null;
095: ResultSet rs = null;
096: conn.setAutoCommit(false);
097:
098: try {
099:
100: createTables(conn, numRows);
101:
102: String selTabSql = "select * from t1";
103:
104: for (int i = 0; i < numPreparedStmts; i++) {
105: ps = conn.prepareStatement(selTabSql);
106: rs = ps.executeQuery();
107:
108: while (rs.next()) {
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: } catch (SQLException e) {
119: TestUtil.dumpSQLExceptions(e);
120: conn.rollback();
121: }
122: }
123:
124: // Tests re-execution of a statement without closing the result
125: // set (DERBY-557).
126: private static void reExecuteStatementTest() throws Exception {
127: System.out.print("DERBY-557: reExecuteStatementTest() ");
128: Connection conn = ij.startJBMS();
129: Statement stmt = conn.createStatement();
130: for (int i = 0; i < 50000; i++) {
131: ResultSet rs = stmt.executeQuery("values(1)");
132: // How silly! I forgot to close the result set.
133: }
134: TestUtil.cleanUpTest(stmt, testObjects);
135: conn.commit();
136: stmt.close();
137: conn.close();
138: System.out.println("PASSED");
139: }
140: }
|