1 package com.db4o.f1.chapter5; 2 3 import java.io.*; 4 import com.db4o.*; 5 import com.db4o.f1.*; 6 7 8 public class ClientServerExample extends Util { 9 private final static int PORT=0xdb40; 10 private final static String USER="user"; 11 private final static String PASSWORD="password"; 12 13 public static void main(String [] args) throws IOException { 14 new File(Util.YAPFILENAME).delete(); 15 accessLocalServer(); 16 new File(Util.YAPFILENAME).delete(); 17 ObjectContainer db=Db4o.openFile(Util.YAPFILENAME); 18 try { 19 setFirstCar(db); 20 setSecondCar(db); 21 } 22 finally { 23 db.close(); 24 } 25 configureDb4o(); 26 ObjectServer server=Db4o.openServer(Util.YAPFILENAME,0); 27 try { 28 queryLocalServer(server); 29 demonstrateLocalReadCommitted(server); 30 demonstrateLocalRollback(server); 31 } 32 finally { 33 server.close(); 34 } 35 accessRemoteServer(); 36 server=Db4o.openServer(Util.YAPFILENAME,PORT); 37 server.grantAccess(USER,PASSWORD); 38 try { 39 queryRemoteServer(PORT,USER,PASSWORD); 40 demonstrateRemoteReadCommitted(PORT,USER,PASSWORD); 41 demonstrateRemoteRollback(PORT,USER,PASSWORD); 42 } 43 finally { 44 server.close(); 45 } 46 } 47 48 public static void setFirstCar(ObjectContainer db) { 49 Pilot pilot=new Pilot("Rubens Barrichello",99); 50 Car car=new Car("BMW"); 51 car.setPilot(pilot); 52 db.set(car); 53 } 54 55 public static void setSecondCar(ObjectContainer db) { 56 Pilot pilot=new Pilot("Michael Schumacher",100); 57 Car car=new Car("Ferrari"); 58 car.setPilot(pilot); 59 db.set(car); 60 } 61 62 public static void accessLocalServer() { 63 ObjectServer server=Db4o.openServer(Util.YAPFILENAME,0); 64 try { 65 ObjectContainer client=server.openClient(); 66 client.close(); 68 } 69 finally { 70 server.close(); 71 } 72 } 73 74 public static void queryLocalServer(ObjectServer server) { 75 ObjectContainer client=server.openClient(); 76 listResult(client.get(new Car(null))); 77 client.close(); 78 } 79 80 public static void configureDb4o() { 81 Db4o.configure().objectClass(Car.class).updateDepth(3); 82 } 83 84 public static void demonstrateLocalReadCommitted(ObjectServer server) { 85 ObjectContainer client1=server.openClient(); 86 ObjectContainer client2=server.openClient(); 87 Pilot pilot=new Pilot("David Coulthard",98); 88 ObjectSet result=client1.get(new Car("BMW")); 89 Car car=(Car)result.next(); 90 car.setPilot(pilot); 91 client1.set(car); 92 listResult(client1.get(new Car(null))); 93 listResult(client2.get(new Car(null))); 94 client1.commit(); 95 listResult(client1.get(Car.class)); 96 listRefreshedResult(client2,client2.get(Car.class),2); 97 client1.close(); 98 client2.close(); 99 } 100 101 public static void demonstrateLocalRollback(ObjectServer server) { 102 ObjectContainer client1=server.openClient(); 103 ObjectContainer client2=server.openClient(); 104 ObjectSet result=client1.get(new Car("BMW")); 105 Car car=(Car)result.next(); 106 car.setPilot(new Pilot("Someone else",0)); 107 client1.set(car); 108 listResult(client1.get(new Car(null))); 109 listResult(client2.get(new Car(null))); 110 client1.rollback(); 111 client1.ext().refresh(car,2); 112 listResult(client1.get(new Car(null))); 113 listResult(client2.get(new Car(null))); 114 client1.close(); 115 client2.close(); 116 } 117 118 public static void accessRemoteServer() throws IOException { 119 ObjectServer server=Db4o.openServer(Util.YAPFILENAME,PORT); 120 server.grantAccess(USER,PASSWORD); 121 try { 122 ObjectContainer client=Db4o.openClient("localhost",PORT,USER,PASSWORD); 123 client.close(); 125 } 126 finally { 127 server.close(); 128 } 129 } 130 131 public static void queryRemoteServer(int port,String user,String password) throws IOException { 132 ObjectContainer client=Db4o.openClient("localhost",port,user,password); 133 listResult(client.get(new Car(null))); 134 client.close(); 135 } 136 137 public static void demonstrateRemoteReadCommitted(int port,String user,String password) throws IOException { 138 ObjectContainer client1=Db4o.openClient("localhost",port,user,password); 139 ObjectContainer client2=Db4o.openClient("localhost",port,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,String password) throws IOException { 155 ObjectContainer client1=Db4o.openClient("localhost",port,user,password); 156 ObjectContainer client2=Db4o.openClient("localhost",port,user,password); 157 ObjectSet result=client1.get(new Car(null)); 158 Car car=(Car)result.next(); 159 car.setPilot(new Pilot("Someone else",0)); 160 client1.set(car); 161 listResult(client1.get(new Car(null))); 162 listResult(client2.get(new Car(null))); 163 client1.rollback(); 164 client1.ext().refresh(car,2); 165 listResult(client1.get(new Car(null))); 166 listResult(client2.get(new Car(null))); 167 client1.close(); 168 client2.close(); 169 } 170 } 171 | Popular Tags |