001: /*
002: * Copyright (C) 2005 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019: package test;
020:
021: import java.sql.Connection;
022: import java.sql.DatabaseMetaData;
023: import java.sql.DriverManager;
024: import java.sql.ResultSet;
025: import java.util.ArrayList;
026:
027: import net.sourceforge.squirrel_sql.fw.sql.SQLUtilities;
028:
029: /**
030: *
031: * A description of this class goes here...
032: */
033: public class DBMetaDataConcurrencyTester {
034:
035: private static String jdbcUrl = "jdbc:oracle:thin:@cumberland:1521:csuite";
036: private static String user = "BELAIR40";
037: private static String pass = "password";
038: private static String tableName = "CS_ACL";
039:
040: private static Connection con = null;
041: private static DatabaseMetaData md = null;
042:
043: private static int iterations = 100;
044: private static int sleepTime = 10;
045: private static int threads = 1;
046: private static boolean getProcedures = false;
047: private static boolean getProductName = false;
048: private static boolean getProductVersion = true;
049: private static boolean getJDBCVersion = false;
050: private static boolean getTables = false;
051: private static boolean getColumns = true;
052:
053: private static boolean printStackTraceOnError = false;
054:
055: private static void init() throws Exception {
056: Class.forName("oracle.jdbc.OracleDriver");
057: con = DriverManager.getConnection(jdbcUrl, user, pass);
058: md = con.getMetaData();
059: }
060:
061: /**
062: * @param args
063: */
064: public static void main(String[] args) throws Exception {
065: init();
066: Thread[] getProceduresThreads = new Thread[threads];
067: Thread[] getProductNameThreads = new Thread[threads];
068: Thread[] getProductVersionThreads = new Thread[threads];
069: Thread[] getJDBCVersionThreads = new Thread[threads];
070: Thread[] getTablesThreads = new Thread[threads];
071: Thread[] getColumnsThreads = new Thread[threads];
072:
073: // create the threads
074: for (int i = 0; i < threads; i++) {
075: getProceduresThreads[i] = create(
076: new GetProceduresRunnable(), i);
077: getProductNameThreads[i] = create(
078: new GetProductNameRunnable(), i);
079: getProductVersionThreads[i] = create(
080: new GetProductVersionRunnable(), i);
081: getJDBCVersionThreads[i] = create(
082: new GetJDBCVersionRunnable(), i);
083: getTablesThreads[i] = create(new GetTablesRunnable(), i);
084: getColumnsThreads[i] = create(new GetColumnsRunnable(), i);
085: }
086:
087: // start the threads
088: for (int i = 0; i < threads; i++) {
089: getProceduresThreads[i].start();
090: getProductNameThreads[i].start();
091: getProductVersionThreads[i].start();
092: getJDBCVersionThreads[i].start();
093: getTablesThreads[i].start();
094: getColumnsThreads[i].start();
095: }
096:
097: // join with the threads
098: for (int i = 0; i < threads; i++) {
099: getProceduresThreads[i].join();
100: getProductNameThreads[i].join();
101: getProductVersionThreads[i].join();
102: getJDBCVersionThreads[i].join();
103: getTablesThreads[i].join();
104: getColumnsThreads[i].join();
105: }
106: }
107:
108: private static Thread create(Runnable runnable, int index) {
109: Thread result = new Thread(runnable);
110: result.setName(runnable.getClass().getName() + index);
111: return result;
112: }
113:
114: private static void handleException(Exception e) {
115: System.err.println(e.getMessage());
116: if (printStackTraceOnError) {
117: e.printStackTrace();
118: }
119: }
120:
121: private static class GetProceduresRunnable implements Runnable {
122:
123: @SuppressWarnings("unused")
124: public void run() {
125: int count = 0;
126: while (getProcedures && count++ < iterations) {
127: ResultSet rs = null;
128: try {
129: System.out.println("Thread "
130: + Thread.currentThread().getName());
131: rs = md.getProcedures(null, user, null);
132: while (rs.next()) {
133: String catalog = rs.getString(1);
134: String schema = rs.getString(2);
135: String name = rs.getString(3);
136: String remarks = rs.getString(7);
137: String type = rs.getString(8);
138: }
139: if (sleepTime > 0) {
140: Thread.sleep(sleepTime);
141: }
142: } catch (Exception e) {
143: handleException(e);
144: } finally {
145: SQLUtilities.closeResultSet(rs);
146: }
147: }
148: }
149: }
150:
151: private static class GetProductNameRunnable implements Runnable {
152:
153: public void run() {
154: int count = 0;
155: while (getProductName && count++ < iterations) {
156: try {
157: System.out.println("Thread "
158: + Thread.currentThread().getName());
159: md.getDatabaseProductName();
160: if (sleepTime > 0) {
161: Thread.sleep(sleepTime);
162: }
163: } catch (Exception e) {
164: handleException(e);
165: }
166: }
167: }
168: }
169:
170: private static class GetProductVersionRunnable implements Runnable {
171:
172: public void run() {
173: int count = 0;
174: while (getProductVersion && count++ < iterations) {
175: try {
176: System.out.println("Thread "
177: + Thread.currentThread().getName());
178: md.getDatabaseProductVersion();
179: if (sleepTime > 0) {
180: Thread.sleep(sleepTime);
181: }
182: } catch (Exception e) {
183: handleException(e);
184: }
185: }
186: }
187: }
188:
189: private static class GetJDBCVersionRunnable implements Runnable {
190:
191: public void run() {
192: int count = 0;
193: while (getJDBCVersion && count++ < iterations) {
194: try {
195: System.out.println("Thread "
196: + Thread.currentThread().getName());
197: md.getJDBCMajorVersion();
198: md.getJDBCMinorVersion();
199: if (sleepTime > 0) {
200: Thread.sleep(sleepTime);
201: }
202: } catch (Exception e) {
203: handleException(e);
204: }
205: }
206: }
207: }
208:
209: private static class GetTablesRunnable implements Runnable {
210: @SuppressWarnings("unused")
211: public void run() {
212: int count = 0;
213: while (getTables && count++ < iterations) {
214: ResultSet rs = null;
215: try {
216: System.out.println("Thread "
217: + Thread.currentThread().getName());
218: rs = md.getTables(null, user, null, null);
219: ArrayList<String> list = new ArrayList<String>();
220: while (rs.next()) {
221: String catalog = rs.getString(1);
222: String schema = rs.getString(2);
223: String name = rs.getString(3);
224: String type = rs.getString(4);
225: String remarks = rs.getString(5);
226: // Oracle yields "Invalid column index" for the following:
227: //String typeCat = rs.getString(6);
228: //String typeSchema = rs.getString(7);
229: //String typeName = rs.getString(8);
230: //String selfRefColName = rs.getString(9);
231: //String refGeneration = rs.getString(10);
232: list.add(name);
233: }
234: //System.out.println("Tables = "+list);
235: if (sleepTime > 0) {
236: Thread.sleep(sleepTime);
237: }
238: } catch (Exception e) {
239: handleException(e);
240: } finally {
241: SQLUtilities.closeResultSet(rs);
242: }
243: }
244: }
245: }
246:
247: private static class GetColumnsRunnable implements Runnable {
248:
249: @SuppressWarnings("unused")
250: public void run() {
251: int count = 0;
252: while (getColumns && count++ < iterations) {
253: ResultSet rs = null;
254: try {
255: System.out.println("Thread "
256: + Thread.currentThread().getName());
257: rs = md.getColumns(null, user, tableName, null);
258: while (rs.next()) {
259: String catalog = rs.getString(1);
260: String schema = rs.getString(2);
261: String tableName = rs.getString(3);
262: String columnName = rs.getString(4);
263: int dataType = rs.getInt(5);
264: String typeName = rs.getString(6);
265: int columnSize = rs.getInt(7);
266: int decimalDigits = rs.getInt(9);
267: int numPrecRadiz = rs.getInt(10);
268: int nullable = rs.getInt(11);
269: String remarks = rs.getString(12);
270: String columnDef = rs.getString(13);
271: int sqlDataType = rs.getInt(14);
272: int sqlDateTimeSub = rs.getInt(15);
273: int charOctetLength = rs.getInt(16);
274: int ordPosition = rs.getInt(17);
275: String isNullable = rs.getString(18);
276: // Oracle yields "Invalid column index" for the following:
277: //String scopeCat = rs.getString(19);
278: //String scopeSchema = rs.getString(20);
279: //String scopeTable = rs.getString(21);
280: //short sourceDataType = rs.getShort(22);
281: }
282: if (sleepTime > 0) {
283: Thread.sleep(sleepTime);
284: }
285: } catch (Exception e) {
286: handleException(e);
287: } finally {
288: SQLUtilities.closeResultSet(rs);
289: }
290: }
291: }
292: }
293:
294: }
|