KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > transactions > Client


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.transactions;
25
26 public class Client
27 {
28     public static int PORT = 54321;
29     public static String JavaDoc USERNAME = "admin";
30     public static String JavaDoc PASSWORD = "admin";
31
32     public static String JavaDoc NEIGHBORHOOD[][] =
33     {
34         {"John", "Smith"}, // john (a.k.a dad)
35
{"Mary", "Smith"}, // mary (a.k.a mom)
36
{"Mark", "Johson"}, // john's neighbor
37
{"Barb", "Johson"}, // john's neighbor
38
{"Jack", "Smith"}, // john's son
39
{"Kate", "Smith"}, // john's daughter
40
{"Jean", "Smith"}, // john's mother
41
};
42
43     public static void main(String JavaDoc args[]) throws Exception JavaDoc
44     {
45         org.myoodb.MyOodbDatabase db = org.myoodb.MyOodbDatabase.open("myoodb://localhost:" + PORT, USERNAME, PASSWORD);
46
47         // XXX: roots cannot be created within a transactions
48

49         Family family = (Family) db.getRoot("The Smith Family");
50
51         if (family == null)
52         {
53             System.out.println("Create The Smith Family");
54
55             family = (Family) db.createRoot(FamilyDbImpl.class, "The Smith Family");
56             //family = (Family) db.createRoot("org.myoodb.transactions.FamilyDbImpl", "The Smith Family");
57
family.setName("Smith");
58         }
59         else
60         {
61             System.out.println("The Smith Family already created");
62         }
63
64         for (int i = 0; i < NEIGHBORHOOD.length; i++)
65         {
66             org.myoodb.MyOodbTransaction tx = db.createTransaction();
67             tx.begin(); /* tx-1 */
68
69             Person person = (Person) db.createObject(PersonDbImpl.class);
70             //Person person = (Person) db.createObject("org.myoodb.transactions.PersonDbImpl");
71
person.setName(NEIGHBORHOOD[i][0]);
72
73             System.out.println(" Checking if Person " + person + " is part of the Smith Family");
74
75             System.out.println(" Is Person locked: " + person.isLocked());
76
77             if (NEIGHBORHOOD[i][1].equals("Smith") == false)
78             {
79                 System.out.println(" Person " + person + " is not part of the Smith Family");
80
81                 tx.rollback(); /* tx-1 */ // make this person disappear!
82

83                 continue;
84             }
85
86             tx.begin(); /* tx-2 */ // show how to nest
87

88             family.add(person);
89
90             if (family.size() > 4)
91             {
92                 System.out.println(" The Smith Family has reached their Maximum size: " + person);
93
94                 tx.rollback(); /* tx-2 */ // undo member add
95

96                 tx.rollback(); /* tx-1 */ // make this person disappear!
97

98                 continue;
99             }
100
101             tx.commit(); /* tx-2 */
102
103             tx.commit(); /* tx-1 */
104
105             System.out.println(" Person " + person + " is now offically part of the Smith Family");
106         }
107     }
108 }
109
Popular Tags