ExtClient is an extended interface, which can be used to access database from the client side. ExtClient object can be simply cast from the ObjectContainer, returned by openClient method:
Java: ExtClient clientExt = (ExtClient) server.openClient()
Please, note that this cast is only possible in embedded server mode.
ExtClient is extending ExtObjectContainer interface. In addition ExtClient provides 3 client-specific methods:
In some special cases you may want to store different objects to different files.
This approach has the following advantages:
To make database switch handling easier you should consider using the ExtClient#switchToFile(fileName) and ExtClient#switchToMainFile() methods. This allows you to use single connection to the server for all database files, thus avoiding overhead of opening and managing new connections (a server starts a new thread for each connection). All you have to do is just point out which database file to use.
01private static void switchExtClients() { 02
new File(DB4O_FILE_NAME).delete(); 03
new File(EXTFILENAME).delete(); 04
ObjectServer server=Db4o.openServer(DB4O_FILE_NAME,0); 05
try { 06
ObjectContainer client=server.openClient(); 07
deleteAll(client); // added to solve sticking objects in doctor 08
Car car = new Car("BMW"); 09
client.set(car); 10
System.out.println("Objects in the main database file:"); 11
retrieveAll(client); 12
13
System.out.println("Switching to additional database:"); 14
ExtClient clientExt = (ExtClient)client; 15
clientExt.switchToFile(EXTFILENAME); 16
car = new Car("Ferrari"); 17
clientExt.set(car); 18
retrieveAll(clientExt); 19
System.out.println("Main database file again: "); 20
clientExt.switchToMainFile(); 21
retrieveAll(clientExt); 22
clientExt.close(); 23
} 24
finally { 25
server.close(); 26
} 27
}
The following can be an example usecase of multiple database usage.
The main database file is used for login, user and rights management only. Multiple satellite database files are used for different applications or multiple user circles. User authorization to the satellite databases is not checked.
Only one single db4o server session needs to be run.