KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > multiple_access > MultipleAccessTest


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-2001 by SMB GmbH. All rights reserved.
6
//
7
// $Id$
8

9 package test.multiple_access;
10
11 import org.ozoneDB.Database;
12 import org.ozoneDB.ExternalDatabase;
13 import test.simple.Garage;
14 import test.simple.GarageImpl;
15 import test.OzoneTestCase;
16
17
18 class AccessThread extends Thread JavaDoc {
19     ExternalDatabase db;
20
21
22     public AccessThread(ExternalDatabase _db) {
23         db = _db;
24     }
25
26
27     public void run() {
28         try {
29             Garage garage = (Garage) db.objectForName("MG");
30             System.out.println("thread(" + hashCode() + "): connected...");
31             garage._populate(new Integer JavaDoc(10));
32             garage.printAll();
33         } catch (Exception JavaDoc e) {
34             e.printStackTrace();
35         }
36     }
37 }
38
39
40 public class MultipleAccessTest extends OzoneTestCase {
41
42
43     public MultipleAccessTest(String JavaDoc name) {
44         super(name);
45     }
46
47     public void testMultipleAccess() {
48         try {
49             // todo: not sure if this is supposed to work for localDatabase
50
ExternalDatabase db = db();
51
52             // LocalDatabase db = new LocalDatabase();
53
// db.open ("/tmp/db", LogWriter.DEBUG3);
54

55             db.reloadClasses();
56
57             ExternalDatabase db2 = ExternalDatabase.openDatabase(getDbUrl());
58             // db2.open ("localhost", 3333);
59

60             Garage garage = (Garage) db.objectForName("MG");
61             if (garage == null) {
62                 garage = (Garage) db.createObject(GarageImpl.class.getName(), Database.Public, "MG");
63             }
64
65             Thread JavaDoc t1 = new AccessThread(db);
66             t1.setPriority(Thread.currentThread().getPriority());
67             t1.setName("t1");
68             t1.start();
69
70             System.out.println("wait...");
71             Thread.sleep(3000);
72             System.out.println("go on...");
73
74             Thread JavaDoc t2 = new AccessThread(db);
75             t2.setPriority(Thread.currentThread().getPriority());
76             t2.setName("t2");
77             t2.start();
78
79             Thread.sleep(1000);
80             while (t1.isAlive() || t2.isAlive()) {
81                 System.out.println("wait for threads...");
82                 Thread.sleep(1000);
83             }
84
85             // db.deleteObject (garage);
86

87             db2.close();
88             System.out.println("deconnected...");
89         } catch (Throwable JavaDoc e) {
90             fail(e.toString());
91         }
92     }
93 }
94
Popular Tags