KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > TestThreadsNLocks


1 package org.apache.ojb.odmg;
2
3 import java.util.Collection JavaDoc;
4
5 import org.apache.ojb.broker.TestHelper;
6 import org.apache.ojb.odmg.shared.Article;
7 import org.odmg.Database;
8 import org.odmg.Implementation;
9 import org.odmg.OQLQuery;
10 import org.odmg.Transaction;
11
12
13 /**
14  * put your documentation comment here
15  */

16 public class TestThreadsNLocks extends Thread JavaDoc
17 {
18
19     private static String JavaDoc databaseName;
20     private static Implementation odmg;
21     private static Database db;
22
23     static
24     {
25         databaseName = TestHelper.DEF_DATABASE_NAME;
26     }
27
28     /**
29      * put your documentation comment here
30      * @param args
31      */

32     public static void main(String JavaDoc[] args)
33     {
34         try
35         {
36
37             // get odmg facade instance
38
odmg = OJB.getInstance();
39             db = odmg.newDatabase();
40             //open database
41

42             db.open(databaseName, Database.OPEN_READ_WRITE);
43
44
45             TestThreadsNLocks test = new TestThreadsNLocks();
46             test.start();
47             TestThreadsNLocks test2 = new TestThreadsNLocks();
48             test2.start();
49         }
50         catch (Exception JavaDoc except)
51         {
52             System.out.println(except);
53             except.printStackTrace(System.out);
54         }
55     }
56
57     /**
58      * put your documentation comment here
59      * @param PrintWriter writer
60      */

61     public TestThreadsNLocks() throws Exception JavaDoc
62     {
63
64     }
65
66     /**
67      * put your documentation comment here
68      */

69     public void run()
70     {
71
72         //System.out.println("The list of available products:");
73
try
74         {
75             // 1. open a transaction
76
Transaction tx = odmg.newTransaction();
77             tx.begin();
78             // 2. get an OQLQuery object from the ODMG facade
79
OQLQuery query = odmg.newOQLQuery();
80             // 3. set the OQL select statement
81
query.create("select all from " + Article.class.getName());
82             // 4. perform the query and store the result in a persistent Collection
83
Collection JavaDoc allArticles = (Collection JavaDoc) query.execute();
84             // 5. now iterate over the result to print each product
85
java.util.Iterator JavaDoc iter = allArticles.iterator();
86             Article a = null;
87             while (iter.hasNext())
88             {
89                 a = (Article) iter.next();
90
91                 if (tx.tryLock(a, Transaction.WRITE))
92                 {
93
94                     //db.makePersistent(a);
95
//System.out.println(super.getName() + " ---- Lock erhalten & warten"+ a);
96
Thread.sleep(1000);
97                     a.setArticleName(super.getName() + a.getArticleId());
98                 }
99                 else
100                 {
101                     //System.out.println(super.getName() + " ---- Konnte lock nicht bekommen" + a);
102
}
103
104             }
105
106             tx.commit();
107             db.close();
108             //System.out.println("DB-close");
109
}
110         catch (Throwable JavaDoc t)
111         {
112             t.printStackTrace();
113             // db.close();
114
}
115     }
116 }
117
118
119
120
121
122
Popular Tags