001: package com.db4o.f1.chapter5;
002:
003: import java.io.*;
004: import com.db4o.*;
005: import com.db4o.f1.*;
006:
007: public class ClientServerExample extends Util {
008: private final static int PORT = 0xdb40;
009: private final static String USER = "user";
010: private final static String PASSWORD = "password";
011:
012: public static void main(String[] args) throws IOException {
013: new File(Util.DB4OFILENAME).delete();
014: accessLocalServer();
015: new File(Util.DB4OFILENAME).delete();
016: ObjectContainer db = Db4o.openFile(Util.DB4OFILENAME);
017: try {
018: setFirstCar(db);
019: setSecondCar(db);
020: } finally {
021: db.close();
022: }
023: configureDb4o();
024: ObjectServer server = Db4o.openServer(Util.DB4OFILENAME, 0);
025: try {
026: queryLocalServer(server);
027: demonstrateLocalReadCommitted(server);
028: demonstrateLocalRollback(server);
029: } finally {
030: server.close();
031: }
032: accessRemoteServer();
033: server = Db4o.openServer(Util.DB4OFILENAME, PORT);
034: server.grantAccess(USER, PASSWORD);
035: try {
036: queryRemoteServer(PORT, USER, PASSWORD);
037: demonstrateRemoteReadCommitted(PORT, USER, PASSWORD);
038: demonstrateRemoteRollback(PORT, USER, PASSWORD);
039: } finally {
040: server.close();
041: }
042: }
043:
044: public static void setFirstCar(ObjectContainer db) {
045: Pilot pilot = new Pilot("Rubens Barrichello", 99);
046: Car car = new Car("BMW");
047: car.setPilot(pilot);
048: db.set(car);
049: }
050:
051: public static void setSecondCar(ObjectContainer db) {
052: Pilot pilot = new Pilot("Michael Schumacher", 100);
053: Car car = new Car("Ferrari");
054: car.setPilot(pilot);
055: db.set(car);
056: }
057:
058: public static void accessLocalServer() {
059: ObjectServer server = Db4o.openServer(Util.DB4OFILENAME, 0);
060: try {
061: ObjectContainer client = server.openClient();
062: // Do something with this client, or open more clients
063: client.close();
064: } finally {
065: server.close();
066: }
067: }
068:
069: public static void queryLocalServer(ObjectServer server) {
070: ObjectContainer client = server.openClient();
071: listResult(client.get(new Car(null)));
072: client.close();
073: }
074:
075: public static void configureDb4o() {
076: Db4o.configure().objectClass(Car.class).updateDepth(3);
077: }
078:
079: public static void demonstrateLocalReadCommitted(ObjectServer server) {
080: ObjectContainer client1 = server.openClient();
081: ObjectContainer client2 = server.openClient();
082: Pilot pilot = new Pilot("David Coulthard", 98);
083: ObjectSet result = client1.get(new Car("BMW"));
084: Car car = (Car) result.next();
085: car.setPilot(pilot);
086: client1.set(car);
087: listResult(client1.get(new Car(null)));
088: listResult(client2.get(new Car(null)));
089: client1.commit();
090: listResult(client1.get(Car.class));
091: listRefreshedResult(client2, client2.get(Car.class), 2);
092: client1.close();
093: client2.close();
094: }
095:
096: public static void demonstrateLocalRollback(ObjectServer server) {
097: ObjectContainer client1 = server.openClient();
098: ObjectContainer client2 = server.openClient();
099: ObjectSet result = client1.get(new Car("BMW"));
100: Car car = (Car) result.next();
101: car.setPilot(new Pilot("Someone else", 0));
102: client1.set(car);
103: listResult(client1.get(new Car(null)));
104: listResult(client2.get(new Car(null)));
105: client1.rollback();
106: client1.ext().refresh(car, 2);
107: listResult(client1.get(new Car(null)));
108: listResult(client2.get(new Car(null)));
109: client1.close();
110: client2.close();
111: }
112:
113: public static void accessRemoteServer() throws IOException {
114: ObjectServer server = Db4o.openServer(Util.DB4OFILENAME, PORT);
115: server.grantAccess(USER, PASSWORD);
116: try {
117: ObjectContainer client = Db4o.openClient("localhost", PORT,
118: USER, PASSWORD);
119: // Do something with this client, or open more clients
120: client.close();
121: } finally {
122: server.close();
123: }
124: }
125:
126: public static void queryRemoteServer(int port, String user,
127: String password) throws IOException {
128: ObjectContainer client = Db4o.openClient("localhost", port,
129: user, password);
130: listResult(client.get(new Car(null)));
131: client.close();
132: }
133:
134: public static void demonstrateRemoteReadCommitted(int port,
135: String user, String password) throws IOException {
136: ObjectContainer client1 = Db4o.openClient("localhost", port,
137: user, password);
138: ObjectContainer client2 = Db4o.openClient("localhost", port,
139: user, password);
140: Pilot pilot = new Pilot("Jenson Button", 97);
141: ObjectSet result = client1.get(new Car(null));
142: Car car = (Car) result.next();
143: car.setPilot(pilot);
144: client1.set(car);
145: listResult(client1.get(new Car(null)));
146: listResult(client2.get(new Car(null)));
147: client1.commit();
148: listResult(client1.get(new Car(null)));
149: listRefreshedResult(client2, client2.get(Car.class), 2);
150: client1.close();
151: client2.close();
152: }
153:
154: public static void demonstrateRemoteRollback(int port, String user,
155: String password) throws IOException {
156: ObjectContainer client1 = Db4o.openClient("localhost", port,
157: user, password);
158: ObjectContainer client2 = Db4o.openClient("localhost", port,
159: user, password);
160: ObjectSet result = client1.get(new Car(null));
161: Car car = (Car) result.next();
162: car.setPilot(new Pilot("Someone else", 0));
163: client1.set(car);
164: listResult(client1.get(new Car(null)));
165: listResult(client2.get(new Car(null)));
166: client1.rollback();
167: client1.ext().refresh(car, 2);
168: listResult(client1.get(new Car(null)));
169: listResult(client2.get(new Car(null)));
170: client1.close();
171: client2.close();
172: }
173: }
|