KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > test > internal > performance > db > DB


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.test.internal.performance.db;
12
13 import java.io.File JavaDoc;
14 import java.sql.Connection JavaDoc;
15 import java.sql.DriverManager JavaDoc;
16 import java.sql.ResultSet JavaDoc;
17 import java.sql.SQLException JavaDoc;
18 import java.sql.Timestamp JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Comparator JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.regex.Matcher JavaDoc;
28 import java.util.regex.Pattern JavaDoc;
29
30 import junit.framework.Assert;
31
32 import org.eclipse.test.internal.performance.InternalPerformanceMeter;
33 import org.eclipse.test.internal.performance.PerformanceTestPlugin;
34 import org.eclipse.test.internal.performance.data.DataPoint;
35 import org.eclipse.test.internal.performance.data.Dim;
36 import org.eclipse.test.internal.performance.data.Sample;
37 import org.eclipse.test.internal.performance.data.Scalar;
38 import org.eclipse.test.performance.Dimension;
39 import org.eclipse.test.performance.Performance;
40
41 public class DB {
42     
43     private static final boolean DEBUG= false;
44     private static final boolean AGGREGATE= true;
45     
46     // the two supported DB types
47
private static final String JavaDoc DERBY= "derby"; //$NON-NLS-1$
48
private static final String JavaDoc CLOUDSCAPE= "cloudscape"; //$NON-NLS-1$
49

50     private static DB fgDefault;
51     
52     private Connection JavaDoc fConnection;
53     private SQL fSQL;
54     private int fStoredSamples;
55     private boolean fStoreCalled;
56     private boolean fIsEmbedded;
57     private String JavaDoc fDBType; // either "derby" or "cloudscape"
58

59     
60     // Datapaoints
61
public static DataPoint[] queryDataPoints(Variations variations, String JavaDoc scenarioName, Set JavaDoc dims) {
62         return getDefault().internalQueryDataPoints(variations, scenarioName, dims);
63     }
64    
65     // Scenarios
66
/**
67      * Return all Scenarios that match the given config, build, and scenario name.
68      * @param configName
69      * @param buildPattern
70      * @param scenarioPattern
71      * @return array of scenarios
72      * @deprecated Use queryScenarios(Variations variations, ...) instead
73      */

74     public static Scenario[] queryScenarios(String JavaDoc configName, String JavaDoc buildPattern, String JavaDoc scenarioPattern) {
75         Variations variations= new Variations();
76         variations.put(PerformanceTestPlugin.CONFIG, configName);
77         variations.put(PerformanceTestPlugin.BUILD, buildPattern);
78         return queryScenarios(variations, scenarioPattern, PerformanceTestPlugin.BUILD, null);
79     }
80
81     /**
82      * @param configName
83      * @param buildPatterns
84      * @param scenarioPattern
85      * @param dimensions
86      * @return array of scenarios
87      * @deprecated Use queryScenarios(Variations variations, ...) instead
88      */

89     public static Scenario[] queryScenarios(String JavaDoc configName, String JavaDoc[] buildPatterns, String JavaDoc scenarioPattern, Dim[] dimensions) {
90         Variations variations= new Variations();
91         variations.put(PerformanceTestPlugin.CONFIG, configName);
92         variations.put(PerformanceTestPlugin.BUILD, buildPatterns);
93         return queryScenarios(variations, scenarioPattern, PerformanceTestPlugin.BUILD, dimensions);
94     }
95
96     /**
97      * @param configName
98      * @param buildPatterns
99      * @param scenarioName
100      * @return Scenario
101      * @deprecated Use queryScenarios(Variations variations, ...) instead
102      */

103     public static Scenario queryScenario(String JavaDoc configName, String JavaDoc[] buildPatterns, String JavaDoc scenarioName) {
104         Variations variations= new Variations();
105         variations.put(PerformanceTestPlugin.CONFIG, configName);
106         variations.put(PerformanceTestPlugin.BUILD, buildPatterns);
107         return new Scenario(scenarioName, variations, PerformanceTestPlugin.BUILD, null);
108     }
109     
110     /**
111      * Returns all Scenarios that match the given variation and scenario pattern.
112      * Every Scenario returned contains a series of datapoints specified by the seriesKey.
113      *
114      * For example to get the datapoints for
115      * For every Scenario only the specified Diemnsions are retrieved from the database.
116      * @param variations
117      * @param scenarioPattern
118      * @param seriesKey
119      * @param dimensions
120      * @return array of scenarios or <code>null</code> if an error occured.
121      */

122     public static Scenario[] queryScenarios(Variations variations, String JavaDoc scenarioPattern, String JavaDoc seriesKey, Dim[] dimensions) {
123         String JavaDoc[] scenarioNames= getDefault().internalQueryScenarioNames(variations, scenarioPattern); // get all Scenario names
124
if (scenarioNames == null)
125             return new Scenario[0];
126         Scenario.SharedState ss= new Scenario.SharedState(variations, scenarioPattern, seriesKey, dimensions);
127         Scenario[] tables= new Scenario[scenarioNames.length];
128         for (int i= 0; i < scenarioNames.length; i++)
129             tables[i]= new Scenario(scenarioNames[i], ss);
130         return tables;
131     }
132
133     /**
134      * Returns all summaries that match the given variation and scenario patterns.
135      * If scenarioPattern is null, all summary scenarios are returned that are marked as "global".
136      * If scenarioPattern is not null, it is used to filter the scenarios and only scenarios marked as "local" are returned.
137      * @param variationPatterns
138      * @param scenarioPattern
139      * @return array of summaries or <code>null</code> if an error occured.
140      */

141     public static SummaryEntry[] querySummaries(Variations variationPatterns, String JavaDoc scenarioPattern) {
142         return getDefault().internalQuerySummaries(variationPatterns, scenarioPattern);
143     }
144
145     /**
146      * @param names
147      * @param variationPatterns
148      * @param scenarioPattern
149      * @deprecated Use queryDistinctValues instead
150      */

151     public static void queryBuildNames(List JavaDoc names, Variations variationPatterns, String JavaDoc scenarioPattern) {
152         getDefault().internalQueryDistinctValues(names, PerformanceTestPlugin.BUILD, variationPatterns, scenarioPattern);
153     }
154
155     public static void queryDistinctValues(List JavaDoc values, String JavaDoc key, Variations variationPatterns, String JavaDoc scenarioPattern) {
156         getDefault().internalQueryDistinctValues(values, key, variationPatterns, scenarioPattern);
157     }
158  
159     public static String JavaDoc[] querySeriesValues(String JavaDoc scenarioName, Variations v, String JavaDoc seriesKey) {
160         return getDefault().internalQuerySeriesValues(v, scenarioName, seriesKey);
161     }
162     
163     public static Scenario getScenarioSeries(String JavaDoc scenarioName, Variations v, String JavaDoc seriesKey, String JavaDoc startBuild, String JavaDoc endBuild, Dim[] dims) {
164         v= (Variations) v.clone();
165         v.put(seriesKey, new String JavaDoc[] { startBuild, endBuild });
166         Scenario.SharedState ss= new Scenario.SharedState(v, scenarioName, seriesKey, dims);
167         Scenario scenario= new Scenario(scenarioName, ss);
168         TimeSeries ts= scenario.getTimeSeries(dims[0]);
169         if (ts.getLength() < 2) {
170             v.put(seriesKey, "%"); //$NON-NLS-1$
171
String JavaDoc[] names= DB.querySeriesValues(scenarioName, v, seriesKey);
172             if (names.length >= 2) {
173                 String JavaDoc start= findClosest(names, startBuild);
174                 String JavaDoc end= findClosest(names, endBuild);
175                 v.put(seriesKey, new String JavaDoc[] { start, end });
176                 scenario= new Scenario(scenarioName, ss);
177             }
178         }
179         return scenario;
180     }
181     
182     public static Map JavaDoc queryFailure(String JavaDoc scenarioPattern, Variations variations) {
183         return getDefault().internalQueryFailure(scenarioPattern, variations);
184     }
185         
186     private static String JavaDoc findClosest(String JavaDoc[] names, String JavaDoc name) {
187         for (int i= 0; i < names.length; i++)
188             if (names[i].equals(name))
189                 return name;
190             
191         Pattern JavaDoc pattern= Pattern.compile("200[3-9][01][0-9][0-3][0-9]"); //$NON-NLS-1$
192
Matcher JavaDoc matcher= pattern.matcher(name); //$NON-NLS-1$
193

194         if (!matcher.find())
195             return name;
196             
197         int x= Integer.parseInt(name.substring(matcher.start(), matcher.end()));
198         int ix= -1;
199         int mind= 0;
200             
201         for (int i= 0; i < names.length; i++) {
202             matcher.reset(names[i]);
203             if (matcher.find()) {
204                 int y= Integer.parseInt(names[i].substring(matcher.start(), matcher.end()));
205                 int d= Math.abs(y-x);
206                 if (ix < 0 || d < mind) {
207                     mind= d;
208                     ix= i;
209                 }
210             }
211          }
212         
213         if (ix >= 0)
214             return names[ix];
215         return name;
216     }
217
218     /**
219      * Store the data contained in the given sample in the database.
220      * The data is tagged with key/value pairs from variations.
221      * @param variations used to tag the data in the database
222      * @param sample the sample to store
223      * @return returns true if data could be stored successfully
224      */

225     public static boolean store(Variations variations, Sample sample) {
226         return getDefault().internalStore(variations, sample);
227     }
228     
229     /**
230      * @param variations used to tag the data in the database
231      * @param sample the sample maked as failed
232      * @param failMesg the reason of the failure
233      */

234     public static void markAsFailed(Variations variations, Sample sample, String JavaDoc failMesg) {
235         getDefault().internalMarkAsFailed(variations, sample, failMesg);
236     }
237     
238     public static Connection JavaDoc getConnection() {
239         return getDefault().fConnection;
240     }
241     
242     public static boolean isActive() {
243         return fgDefault != null && fgDefault.getSQL() != null;
244     }
245     
246     //---- private implementation
247

248     /**
249      * Private constructor to block instance creation.
250      */

251     private DB() {
252         // empty implementation
253
}
254
255     synchronized static DB getDefault() {
256         if (fgDefault == null) {
257             fgDefault= new DB();
258             fgDefault.connect();
259             if (PerformanceTestPlugin.getDefault() == null) {
260                 // not started as plugin
261
Runtime.getRuntime().addShutdownHook(
262                     new Thread JavaDoc() {
263                         public void run() {
264                             shutdown();
265                         }
266                     }
267                 );
268             }
269         }
270         return fgDefault;
271     }
272     
273     public static void shutdown() {
274         if (DEBUG) System.out.println("DB.shutdown"); //$NON-NLS-1$
275
if (fgDefault != null) {
276             fgDefault.disconnect();
277             fgDefault= null;
278         }
279     }
280    
281     SQL getSQL() {
282         return fSQL;
283     }
284     
285     private void internalMarkAsFailed(Variations variations, Sample sample, String JavaDoc failMesg) {
286         
287         if (fSQL == null)
288             return;
289   
290         try {
291             int variation_id= fSQL.getVariations(variations);
292             int scenario_id= fSQL.getScenario(sample.getScenarioID());
293
294             fSQL.insertFailure(variation_id, scenario_id, failMesg);
295             
296             fConnection.commit();
297             
298         } catch (SQLException JavaDoc e) {
299             PerformanceTestPlugin.log(e);
300             try {
301                 fConnection.rollback();
302             } catch (SQLException JavaDoc e1) {
303                 PerformanceTestPlugin.log(e1);
304             }
305         }
306     }
307
308     private boolean internalStore(Variations variations, Sample sample) {
309         
310         if (fSQL == null || sample == null)
311             return false;
312         
313         DataPoint[] dataPoints= sample.getDataPoints();
314         int n= dataPoints.length;
315         if (n <= 0)
316             return false;
317
318         Scalar[] sc= null;
319         long[] averages= null;
320         if (AGGREGATE) {
321
322             sc= dataPoints[0].getScalars();
323             averages= new long[sc.length];
324
325             Set JavaDoc set= new HashSet JavaDoc();
326             for (int j= 0; j < dataPoints.length; j++) {
327                 DataPoint dp= dataPoints[j];
328                 set.add(new Integer JavaDoc(dp.getStep()));
329             }
330             switch (set.size()) {
331             case 2: // BEFORE/AFTER pairs -> calculate deltas
332
for (int i= 0; i < dataPoints.length-1; i+= 2) {
333                     Scalar[] s1= dataPoints[i].getScalars();
334                     Scalar[] s2= dataPoints[i+1].getScalars();
335                     for (int j= 0; j < sc.length; j++)
336                         averages[j] += s2[j].getMagnitude() - s1[j].getMagnitude();
337                 }
338                 n= n/2;
339                 break;
340                 
341             case 1: // single values
342
for (int i= 0; i < dataPoints.length; i++) {
343                     Scalar[] s= dataPoints[i].getScalars();
344                     for (int j= 0; j < sc.length; j++)
345                         averages[j] += s[j].getMagnitude();
346                 }
347                 break;
348                 
349             default: // not expected
350
PerformanceTestPlugin.logError("DB.internalStore: too many steps in DataPoint"); //$NON-NLS-1$
351
return false;
352             }
353         }
354         
355         //System.out.println("store started..."); //$NON-NLS-1$
356
try {
357             //long l= System.currentTimeMillis();
358
int variation_id= fSQL.getVariations(variations);
359             int scenario_id= fSQL.getScenario(sample.getScenarioID());
360             if (sample.isSummary()) {
361                 boolean isGlobal= sample.isGlobal();
362                 
363                 int commentId= 0;
364                 int commentKind= sample.getCommentType();
365                 String JavaDoc comment= sample.getComment();
366                 if (commentKind == Performance.EXPLAINS_DEGRADATION_COMMENT && comment != null)
367                         commentId= fSQL.getCommentId(commentKind, comment);
368                 
369                 Dimension[] summaryDimensions= sample.getSummaryDimensions();
370                 for (int i= 0; i < summaryDimensions.length; i++) {
371                     Dimension dimension= summaryDimensions[i];
372                     if (dimension instanceof Dim)
373                         fSQL.createSummaryEntry(variation_id, scenario_id, ((Dim)dimension).getId(), isGlobal, commentId);
374                 }
375                 String JavaDoc shortName= sample.getShortname();
376                 if (shortName != null)
377                     fSQL.setScenarioShortName(scenario_id, shortName);
378             }
379             int sample_id= fSQL.createSample(variation_id, scenario_id, new Timestamp JavaDoc(sample.getStartTime()));
380
381             if (sc != null) {
382                 int datapoint_id= fSQL.createDataPoint(sample_id, 0, InternalPerformanceMeter.AVERAGE);
383                 for (int k= 0; k < sc.length; k++) {
384                     int dim_id= sc[k].getDimension().getId();
385                     fSQL.insertScalar(datapoint_id, dim_id, averages[k] / n);
386                 }
387             } else {
388                 for (int i= 0; i < dataPoints.length; i++) {
389                     DataPoint dp= dataPoints[i];
390                     int datapoint_id= fSQL.createDataPoint(sample_id, i, dp.getStep());
391                     Scalar[] scalars= dp.getScalars();
392                     for (int j= 0; j < scalars.length; j++) {
393                         Scalar scalar= scalars[j];
394                         int dim_id= scalar.getDimension().getId();
395                         long value= scalar.getMagnitude();
396                         fSQL.insertScalar(datapoint_id, dim_id, value);
397                     }
398                 }
399             }
400             
401             fConnection.commit();
402             fStoredSamples++;
403             fStoreCalled= true;
404
405             //System.err.println(System.currentTimeMillis()-l);
406

407         } catch (SQLException JavaDoc e) {
408             PerformanceTestPlugin.log(e);
409             try {
410                 fConnection.rollback();
411             } catch (SQLException JavaDoc e1) {
412                 PerformanceTestPlugin.log(e1);
413             }
414         }
415         return true;
416     }
417     
418     private DataPoint[] internalQueryDataPoints(Variations variations, String JavaDoc scenarioName, Set JavaDoc dimSet) {
419         if (fSQL == null)
420             return null;
421         
422         ResultSet JavaDoc rs= null;
423         try {
424             ArrayList JavaDoc dataPoints= new ArrayList JavaDoc();
425             rs= fSQL.queryDataPoints(variations, scenarioName);
426             while (rs.next()) {
427                 int datapoint_id= rs.getInt(1);
428                 int step= rs.getInt(2);
429
430                 HashMap JavaDoc map= new HashMap JavaDoc();
431                 ResultSet JavaDoc rs2= fSQL.queryScalars(datapoint_id);
432                 while (rs2.next()) {
433                     int dim_id= rs2.getInt(1);
434                     long value= rs2.getBigDecimal(2).longValue();
435                     Dim dim= Dim.getDimension(dim_id);
436                     if (dim != null) {
437                         if (dimSet == null || dimSet.contains(dim))
438                             map.put(dim, new Scalar(dim, value));
439                     }
440                 }
441                 if (map.size() > 0)
442                     dataPoints.add(new DataPoint(step, map));
443                 
444                 rs2.close();
445             }
446             rs.close();
447             
448             int n= dataPoints.size();
449             if (DEBUG) System.out.println("query resulted in " + n + " datapoints from DB"); //$NON-NLS-1$ //$NON-NLS-2$
450
return (DataPoint[])dataPoints.toArray(new DataPoint[n]);
451
452         } catch (SQLException JavaDoc e) {
453             PerformanceTestPlugin.log(e);
454
455         } finally {
456             if (rs != null)
457                 try {
458                     rs.close();
459                 } catch (SQLException JavaDoc e1) {
460                     // ignored
461
}
462         }
463         return null;
464     }
465     
466     /*
467      * Returns array of scenario names matching the given pattern.
468      */

469     private String JavaDoc[] internalQueryScenarioNames(Variations variations, String JavaDoc scenarioPattern) {
470         if (fSQL == null)
471             return null;
472         ResultSet JavaDoc result= null;
473         try {
474             result= fSQL.queryScenarios(variations, scenarioPattern);
475             ArrayList JavaDoc scenarios= new ArrayList JavaDoc();
476             for (int i= 0; result.next(); i++)
477                 scenarios.add(result.getString(1));
478             return (String JavaDoc[])scenarios.toArray(new String JavaDoc[scenarios.size()]);
479
480         } catch (SQLException JavaDoc e) {
481             PerformanceTestPlugin.log(e);
482
483         } finally {
484             if (result != null)
485                 try {
486                     result.close();
487                 } catch (SQLException JavaDoc e1) {
488                     // ignored
489
}
490         }
491         return null;
492     }
493     
494     /*
495      *
496      */

497     private void internalQueryDistinctValues(List JavaDoc values, String JavaDoc seriesKey, Variations variations, String JavaDoc scenarioPattern) {
498         if (fSQL == null)
499             return;
500         ResultSet JavaDoc result= null;
501         try {
502             result= fSQL.queryVariations(variations.toExactMatchString(), scenarioPattern);
503             for (int i= 0; result.next(); i++) {
504                 Variations v= new Variations();
505                 v.parseDB(result.getString(1));
506                 String JavaDoc build= v.getProperty(seriesKey);
507                 if (build != null && !values.contains(build))
508                     values.add(build);
509             }
510         } catch (SQLException JavaDoc e) {
511             PerformanceTestPlugin.log(e);
512
513         } finally {
514             if (result != null)
515                 try {
516                     result.close();
517                 } catch (SQLException JavaDoc e1) {
518                     // ignored
519
}
520         }
521     }
522     
523     private SummaryEntry[] internalQuerySummaries(Variations variationPatterns, String JavaDoc scenarioPattern) {
524         if (fSQL == null)
525             return null;
526         ResultSet JavaDoc result= null;
527         try {
528             List JavaDoc fingerprints= new ArrayList JavaDoc();
529             ResultSet JavaDoc rs;
530             if (scenarioPattern != null)
531                 rs= fSQL.querySummaryEntries(variationPatterns, scenarioPattern);
532             else
533                 rs= fSQL.queryGlobalSummaryEntries(variationPatterns);
534             while (rs.next()) {
535                 String JavaDoc scenarioName= rs.getString(1);
536                 String JavaDoc shortName= rs.getString(2);
537                 int dim_id= rs.getInt(3);
538                 boolean isGlobal= rs.getShort(4) == 1;
539                 int comment_id= rs.getInt(5);
540                 int commentKind= 0;
541                 String JavaDoc comment= null;
542                 if (comment_id != 0) {
543                         ResultSet JavaDoc rs2= fSQL.getComment(comment_id);
544                         if (rs2.next()) {
545                             commentKind= rs2.getInt(1);
546                             comment= rs2.getString(2);
547                         }
548                 }
549                 fingerprints.add(new SummaryEntry(scenarioName, shortName, Dim.getDimension(dim_id), isGlobal, commentKind, comment));
550             }
551             return (SummaryEntry[])fingerprints.toArray(new SummaryEntry[fingerprints.size()]);
552         } catch (SQLException JavaDoc e) {
553             PerformanceTestPlugin.log(e);
554
555         } finally {
556             if (result != null)
557                 try {
558                     result.close();
559                 } catch (SQLException JavaDoc e1) {
560                     // ignored
561
}
562         }
563         return null;
564     }
565     
566     private String JavaDoc[] internalQuerySeriesValues(Variations v, String JavaDoc scenarioName, String JavaDoc seriesKey) {
567         
568         boolean isCloned= false;
569         
570         String JavaDoc[] seriesPatterns= null;
571         Object JavaDoc object= v.get(seriesKey);
572         if (object instanceof String JavaDoc[])
573             seriesPatterns= (String JavaDoc[]) object;
574         else if (object instanceof String JavaDoc)
575             seriesPatterns= new String JavaDoc[] { (String JavaDoc) object };
576         else
577             Assert.assertTrue(false);
578         
579         ArrayList JavaDoc values= new ArrayList JavaDoc();
580         for (int i= 0; i < seriesPatterns.length; i++) {
581             if (seriesPatterns[i].indexOf('%') >= 0) {
582                 if (! isCloned) {
583                     v= (Variations) v.clone();
584                     isCloned= true;
585                 }
586                 v.put(seriesKey, seriesPatterns[i]);
587                 internalQueryDistinctValues(values, seriesKey, v, scenarioName);
588             } else
589                 values.add(seriesPatterns[i]);
590         }
591         
592         String JavaDoc[] names= (String JavaDoc[])values.toArray(new String JavaDoc[values.size()]);
593         
594         boolean sort= true;
595         Pattern JavaDoc pattern= Pattern.compile("200[3-9][01][0-9][0-3][0-9]"); //$NON-NLS-1$
596
final Matcher JavaDoc matcher= pattern.matcher(""); //$NON-NLS-1$
597
for (int i= 0; i < names.length; i++) {
598             matcher.reset(names[i]);
599             if (! matcher.find()) {
600                 sort= false;
601                 break;
602             }
603         }
604         if (sort) {
605             Arrays.sort(names,
606                 new Comparator JavaDoc() {
607                     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
608                         String JavaDoc s1= (String JavaDoc)o1;
609                         String JavaDoc s2= (String JavaDoc)o2;
610                         
611                         matcher.reset(s1);
612                         if (matcher.find())
613                             s1= s1.substring(matcher.start());
614
615                         matcher.reset(s2);
616                         if (matcher.find())
617                             s2= s2.substring(matcher.start());
618
619                         return s1.compareTo(s2);
620                     }
621                 }
622             );
623         }
624         return names;
625     }
626
627     private Map JavaDoc internalQueryFailure(String JavaDoc scenarioPattern, Variations variations) {
628         if (fSQL == null)
629             return null;
630         ResultSet JavaDoc result= null;
631         try {
632             Map JavaDoc map= new HashMap JavaDoc();
633             result= fSQL.queryFailure(variations, scenarioPattern);
634             while (result.next()) {
635                 String JavaDoc scenario= result.getString(1);
636                 String JavaDoc message= result.getString(2);
637                 map.put(scenario, message);
638             }
639             return map;
640         } catch (SQLException JavaDoc e) {
641             PerformanceTestPlugin.log(e);
642
643         } finally {
644             if (result != null)
645                 try {
646                     result.close();
647                 } catch (SQLException JavaDoc e1) {
648                     // ignored
649
}
650         }
651         return null;
652     }
653
654     /**
655      * dbloc= embed in home directory
656      * dbloc=/tmp/performance embed given location
657      * dbloc=net://localhost connect to local server
658      * dbloc=net://www.eclipse.org connect to remove server
659      */

660     private void connect() {
661
662         if (fConnection != null)
663             return;
664
665         String JavaDoc dbloc= PerformanceTestPlugin.getDBLocation();
666         if (dbloc == null)
667             return;
668                    
669         String JavaDoc dbname= PerformanceTestPlugin.getDBName();
670         String JavaDoc url= null;
671         java.util.Properties JavaDoc info= new java.util.Properties JavaDoc();
672         
673         fDBType= DERBY; // assume we are using Derby
674
try {
675             if (dbloc.startsWith("net://")) { //$NON-NLS-1$
676
// remote
677
fIsEmbedded= false;
678                 // connect over network
679
if (DEBUG) System.out.println("Trying to connect over network..."); //$NON-NLS-1$
680
Class.forName("com.ibm.db2.jcc.DB2Driver"); //$NON-NLS-1$
681
info.put("user", PerformanceTestPlugin.getDBUser()); //$NON-NLS-1$
682
info.put("password", PerformanceTestPlugin.getDBPassword()); //$NON-NLS-1$
683
info.put("retrieveMessagesFromServerOnGetMessage", "true"); //$NON-NLS-1$ //$NON-NLS-2$
684
url= dbloc + "/" + dbname + ";create=true"; //$NON-NLS-1$//$NON-NLS-2$
685
} else {
686                 
687                 // workaround for Derby issue: http://nagoya.apache.org/jira/browse/DERBY-1
688
if ("Mac OS X".equals(System.getProperty("os.name"))) //$NON-NLS-1$//$NON-NLS-2$
689
System.setProperty("derby.storage.fileSyncTransactionLog", "true"); //$NON-NLS-1$ //$NON-NLS-2$
690

691                 // embedded
692
fIsEmbedded= true;
693                 try {
694                     Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); //$NON-NLS-1$
695
} catch (ClassNotFoundException JavaDoc e) {
696                     Class.forName("com.ihost.cs.jdbc.CloudscapeDriver"); //$NON-NLS-1$
697
fDBType= CLOUDSCAPE;
698                 }
699                 if (DEBUG) System.out.println("Loaded embedded " + fDBType); //$NON-NLS-1$
700
File JavaDoc f;
701                 if (dbloc.length() == 0) {
702                     String JavaDoc user_home= System.getProperty("user.home"); //$NON-NLS-1$
703
if (user_home == null)
704                         return;
705                     f= new File JavaDoc(user_home, fDBType);
706                 } else
707                     f= new File JavaDoc(dbloc);
708                 url= new File JavaDoc(f, dbname).getAbsolutePath();
709                 info.put("create", "true"); //$NON-NLS-1$ //$NON-NLS-2$
710
}
711             try {
712                 fConnection= DriverManager.getConnection("jdbc:" + fDBType + ":" + url, info); //$NON-NLS-1$ //$NON-NLS-2$
713
} catch (SQLException JavaDoc e) {
714                 if ("08001".equals(e.getSQLState()) && DERBY.equals(fDBType)) { //$NON-NLS-1$
715
if (DEBUG) System.out.println("DriverManager.getConnection failed; retrying for cloudscape"); //$NON-NLS-1$
716
// try Cloudscape
717
fDBType= CLOUDSCAPE;
718                     fConnection= DriverManager.getConnection("jdbc:" + fDBType + ":" + url, info); //$NON-NLS-1$ //$NON-NLS-2$
719
} else
720                     throw e;
721             }
722             if (DEBUG) System.out.println("connect succeeded!"); //$NON-NLS-1$
723

724             fConnection.setAutoCommit(false);
725             fSQL= new SQL(fConnection);
726             fConnection.commit();
727
728         } catch (SQLException JavaDoc ex) {
729             PerformanceTestPlugin.logError(ex.getMessage());
730
731         } catch (ClassNotFoundException JavaDoc e) {
732             PerformanceTestPlugin.log(e);
733         }
734     }
735     
736     private void disconnect() {
737         if (DEBUG && fStoreCalled)
738             System.out.println("stored " + fStoredSamples + " new datapoints in DB"); //$NON-NLS-1$ //$NON-NLS-2$
739
if (DEBUG) System.out.println("disconnecting from DB"); //$NON-NLS-1$
740
if (fSQL != null) {
741             try {
742                 fSQL.dispose();
743             } catch (SQLException JavaDoc e1) {
744                 PerformanceTestPlugin.log(e1);
745             }
746             fSQL= null;
747         }
748         if (fConnection != null) {
749             try {
750                 fConnection.commit();
751             } catch (SQLException JavaDoc e) {
752                 PerformanceTestPlugin.log(e);
753             }
754             try {
755                  fConnection.close();
756             } catch (SQLException JavaDoc e) {
757                 PerformanceTestPlugin.log(e);
758             }
759             fConnection= null;
760         }
761         
762         if (fIsEmbedded) {
763             try {
764                 DriverManager.getConnection("jdbc:" + fDBType + ":;shutdown=true"); //$NON-NLS-1$ //$NON-NLS-2$
765
} catch (SQLException JavaDoc e) {
766                 String JavaDoc message= e.getMessage();
767                 if (message.indexOf("system shutdown.") < 0) //$NON-NLS-1$
768
e.printStackTrace();
769             }
770         }
771     }
772 }
773
Popular Tags