KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.simpleThread
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.Statement JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.ResultSetMetaData JavaDoc;
28 import java.sql.DriverManager JavaDoc;
29 import org.apache.derby.tools.ij;
30
31 /*
32     This is from a bug found by a beta customer.
33  */

34 public class simpleThread implements Runnable JavaDoc {
35
36         private static Connection JavaDoc _connection = null;
37         private static boolean _inUse = false;
38         private static Object JavaDoc _lock = new Object JavaDoc();
39
40         private long _wait = 0;
41         private long _myCount = 0;
42         private static int _count = 0;
43         private synchronized static int getCount() { return(_count++); }
44         private String JavaDoc _query;
45
46         public simpleThread( String JavaDoc query, long waitTime) {
47                 _wait = waitTime;
48                 _myCount = getCount();
49                 _query = query;
50                 new Thread JavaDoc(this).start();
51         }
52
53         public void run() {
54                 int rows = 0;
55                 boolean caught = false;
56                 try {
57                         Thread.currentThread().sleep(_wait);
58                         Connection JavaDoc conn = GetConnection();
59                         Statement JavaDoc stmt = conn.createStatement();
60                         String JavaDoc query = _query;
61                         ResultSet JavaDoc rs = stmt.executeQuery( query );
62                         ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
63                         //int cols = rsmd.getColumnCount();
64
while(rs.next()) {
65                             rows++;
66                                 //System.out.print(_myCount + ":");
67
//for( int x=0;x<cols;x++) {
68
// String s = rs.getString(x+1);
69
// if( x > 0) System.out.print(",");
70
// System.out.print(s);
71
//}
72
//System.out.println();
73
}
74                         stmt.close();
75                         ReturnConnection(conn);
76                 } catch (Exception JavaDoc ex) {
77                     // we expect some threads to get exceptions
78
caught = true;
79                 }
80                 if (rows == 3 || caught)
81                 {
82                     //System.out.println("This thread's okay!");
83
}
84                 else
85                 {
86                     System.out.println("FAIL: thread "+_myCount+" only got "+rows+" rows and caught was "+caught);
87                 }
88         }
89
90
91         public simpleThread(String JavaDoc argv[]) throws Exception JavaDoc {
92             
93             ij.getPropertyArg(argv);
94             _connection = ij.startJBMS();
95
96             Connection JavaDoc conn = GetConnection();
97
98             Statement JavaDoc stmt = conn.createStatement();
99
100             stmt.execute("create table people(name varchar(255), address varchar(255), phone varchar(64))");
101             stmt.execute("insert into people VALUES ('mike', 'mikes address', '123-456-7890')");
102             stmt.execute("insert into people VALUES ('adam', 'adams address', '123-456-1234')");
103             stmt.execute("insert into people VALUES ('steve', 'steves address', '123-456-4321')");
104             stmt.close();
105
106             ReturnConnection(conn);
107
108             String JavaDoc query = "SELECT * from people ORDER by name";
109
110             try {
111                 String JavaDoc[] retval = new String JavaDoc[4];
112                 new simpleThread(query,0);
113                 new simpleThread(query,10000);
114                 new simpleThread(query,10100);
115                 new simpleThread(query,20000);
116             } catch (Exception JavaDoc ex) {
117                 System.err.println(ex.toString() );
118             }
119         }
120
121         public static Connection JavaDoc GetConnection() {
122                 synchronized(_lock) {
123                         _inUse = true;
124                 }
125                 return _connection;
126         }
127         public static void ReturnConnection(Connection JavaDoc c) {
128                 synchronized(_lock) {
129                         _inUse = false;
130                         _lock.notifyAll();
131                 }
132         }
133 }
134
Popular Tags