001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.lang.simpleThread
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.lang;
023:
024: import java.sql.Connection;
025: import java.sql.Statement;
026: import java.sql.ResultSet;
027: import java.sql.ResultSetMetaData;
028: import java.sql.DriverManager;
029: import org.apache.derby.tools.ij;
030:
031: /*
032: This is from a bug found by a beta customer.
033: */
034: public class simpleThread implements Runnable {
035:
036: private static Connection _connection = null;
037: private static boolean _inUse = false;
038: private static Object _lock = new Object();
039:
040: private long _wait = 0;
041: private long _myCount = 0;
042: private static int _count = 0;
043:
044: private synchronized static int getCount() {
045: return (_count++);
046: }
047:
048: private String _query;
049:
050: public simpleThread(String query, long waitTime) {
051: _wait = waitTime;
052: _myCount = getCount();
053: _query = query;
054: new Thread(this ).start();
055: }
056:
057: public void run() {
058: int rows = 0;
059: boolean caught = false;
060: try {
061: Thread.currentThread().sleep(_wait);
062: Connection conn = GetConnection();
063: Statement stmt = conn.createStatement();
064: String query = _query;
065: ResultSet rs = stmt.executeQuery(query);
066: ResultSetMetaData rsmd = rs.getMetaData();
067: //int cols = rsmd.getColumnCount();
068: while (rs.next()) {
069: rows++;
070: //System.out.print(_myCount + ":");
071: //for( int x=0;x<cols;x++) {
072: // String s = rs.getString(x+1);
073: // if( x > 0) System.out.print(",");
074: // System.out.print(s);
075: //}
076: //System.out.println();
077: }
078: stmt.close();
079: ReturnConnection(conn);
080: } catch (Exception ex) {
081: // we expect some threads to get exceptions
082: caught = true;
083: }
084: if (rows == 3 || caught) {
085: //System.out.println("This thread's okay!");
086: } else {
087: System.out.println("FAIL: thread " + _myCount
088: + " only got " + rows + " rows and caught was "
089: + caught);
090: }
091: }
092:
093: public simpleThread(String argv[]) throws Exception {
094:
095: ij.getPropertyArg(argv);
096: _connection = ij.startJBMS();
097:
098: Connection conn = GetConnection();
099:
100: Statement stmt = conn.createStatement();
101:
102: stmt
103: .execute("create table people(name varchar(255), address varchar(255), phone varchar(64))");
104: stmt
105: .execute("insert into people VALUES ('mike', 'mikes address', '123-456-7890')");
106: stmt
107: .execute("insert into people VALUES ('adam', 'adams address', '123-456-1234')");
108: stmt
109: .execute("insert into people VALUES ('steve', 'steves address', '123-456-4321')");
110: stmt.close();
111:
112: ReturnConnection(conn);
113:
114: String query = "SELECT * from people ORDER by name";
115:
116: try {
117: String[] retval = new String[4];
118: new simpleThread(query, 0);
119: new simpleThread(query, 10000);
120: new simpleThread(query, 10100);
121: new simpleThread(query, 20000);
122: } catch (Exception ex) {
123: System.err.println(ex.toString());
124: }
125: }
126:
127: public static Connection GetConnection() {
128: synchronized (_lock) {
129: _inUse = true;
130: }
131: return _connection;
132: }
133:
134: public static void ReturnConnection(Connection c) {
135: synchronized (_lock) {
136: _inUse = false;
137: _lock.notifyAll();
138: }
139: }
140: }
|