///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
//
// All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License and GNU Library
// General Public License as published by the Free Software Foundation;
// either version 2, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License and GNU Library General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License
// and GNU Library General Public License along with this program; if
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
// MA 02139, USA.
//
///////////////////////////////////////////////////////////////////////////////
package org.myoodb.transactions;
import org.myoodb.objects.*;
public class Client
{
public static int PORT = 54321;
public static String USERNAME = "admin";
public static String PASSWORD = "admin";
public static String NEIGHBORHOOD[][] =
{
{"John", "Smith"}, // john (a.k.a dad)
{"Mary", "Smith"}, // mary (a.k.a mom)
{"Mark", "Johson"}, // john's neighbor
{"Barb", "Johson"}, // john's neighbor
{"Jack", "Smith"}, // john's son
{"Kate", "Smith"}, // john's daughter
{"Jean", "Smith"}, // john's mother
};
public static void main(String args[]) throws Exception
{
org.myoodb.MyOodbDatabase db = org.myoodb.MyOodbDatabase.open("tcp://localhost:" + PORT, USERNAME, PASSWORD);
// XXX: roots cannot be created within a transactions
Family family = (Family) db.getRoot("Family");
if (family == null)
{
System.out.println("Create The Smith Family");
family = (Family) db.createRoot("Family", "org.myoodb.objects.FamilyDbImpl");
//family = (Family) db.createRoot("Family", FamilyDbImpl.class);
family.setName("Smith");
}
else
{
System.out.println("The Smith Family already created");
}
for (int i = 0; i < NEIGHBORHOOD.length; i++)
{
org.myoodb.MyOodbTransaction tx = db.createTransaction();
tx.begin(); /* tx-1 */
Person person = (Person) db.createObject("org.myoodb.objects.PersonDbImpl");
//Person person = (Person) db.createObject(PersonDbImpl.class);
person.setName(NEIGHBORHOOD[i][0]);
System.out.println(" Checking if Person " + person + " is part of the Smith Family");
System.out.println(" Is Person locked: " + person.isLocked());
if (NEIGHBORHOOD[i][1].equals("Smith") == false)
{
System.out.println(" Person " + person + " is not part of the Smith Family");
tx.rollback(); /* tx-1 */ // make this person disappear!
continue;
}
tx.begin(); /* tx-2 */ // show how to nest
family.add(person);
if (family.size() > 4)
{
System.out.println(" The Smith Family has reached their Maximum size: " + person);
tx.rollback(); /* tx-2 */ // undo member add
tx.rollback(); /* tx-1 */ // make this person disappear!
continue;
}
tx.commit(); /* tx-2 */
tx.commit(); /* tx-1 */
System.out.println(" Person " + person + " is now offically part of the Smith Family");
}
}
}
|