Network Server

In order to make the embedded server operate over a TCP/IP network, we just need to specify a port number greater than zero and set up one or more accounts for our client(s).

ClientServerExample.java: accessRemoteServer
01private static void accessRemoteServer() throws IOException { 02 ObjectServer server=Db4o.openServer(DB4O_FILE_NAME,PORT); 03 server.grantAccess(USER,PASSWORD); 04 try { 05 ObjectContainer client=Db4o.openClient("localhost",PORT,USER,PASSWORD); 06 // Do something with this client, or open more clients 07 client.close(); 08 } 09 finally { 10 server.close(); 11 } 12 }

The client connects providing host, port, user name and password.

ClientServerExample.java: queryRemoteServer
1private static void queryRemoteServer(int port,String user,String password) throws IOException { 2 ObjectContainer client=Db4o.openClient("localhost",port,user,password); 3 listResult(client.get(new Car(null))); 4 client.close(); 5 }

Everything else is absolutely identical to the local server examples.

ClientServerExample.java: demonstrateRemoteReadCommitted
01private static void demonstrateRemoteReadCommitted(int port,String user,String password) throws IOException { 02 ObjectContainer client1=Db4o.openClient("localhost",port,user,password); 03 ObjectContainer client2=Db4o.openClient("localhost",port,user,password); 04 Pilot pilot=new Pilot("Jenson Button",97); 05 ObjectSet result=client1.get(new Car(null)); 06 Car car=(Car)result.next(); 07 car.setPilot(pilot); 08 client1.set(car); 09 listResult(client1.get(new Car(null))); 10 listResult(client2.get(new Car(null))); 11 client1.commit(); 12 listResult(client1.get(new Car(null))); 13 listRefreshedResult(client2,client2.get(Car.class),2); 14 client1.close(); 15 client2.close(); 16 }
ClientServerExample.java: demonstrateRemoteRollback
01private static void demonstrateRemoteRollback(int port,String user,String password) throws IOException { 02 ObjectContainer client1=Db4o.openClient("localhost",port,user,password); 03 ObjectContainer client2=Db4o.openClient("localhost",port,user,password); 04 ObjectSet result=client1.get(new Car(null)); 05 Car car=(Car)result.next(); 06 car.setPilot(new Pilot("Someone else",0)); 07 client1.set(car); 08 listResult(client1.get(new Car(null))); 09 listResult(client2.get(new Car(null))); 10 client1.rollback(); 11 client1.ext().refresh(car,2); 12 listResult(client1.get(new Car(null))); 13 listResult(client2.get(new Car(null))); 14 client1.close(); 15 client2.close(); 16 }