Batch Messaging Example

Let's create a small example to test batch messaging mode behavior. We will use bulk insert with and without batch messaging configuration.

BatchExample.java: fillUpDb
01private static void fillUpDb(ObjectContainer container) { 02 System.out.println("Testing inserts"); 03 long t1 = System.currentTimeMillis(); 04 for (int i = 0; i < NO_OF_OBJECTS; i++){ 05 Pilot pilot = new Pilot("pilot #"+ i, i); 06 container.set(pilot); 07 } 08 long t2 = System.currentTimeMillis(); 09 long diff = t2 - t1; 10 System.out.println("Operation time: " + diff + " ms."); 11 }

Let's configure the server and run the insert operation first without batch messages, then with batch messages:

BatchExample.java: main
01public static void main(String[] args) throws IOException { 02 ObjectServer db4oServer = Db4o.openServer(FILE, PORT); 03 try { 04 db4oServer.grantAccess(USER, PASS); 05 ObjectContainer container = Db4o.openClient(HOST, PORT, USER, 06 PASS); 07 try { 08 fillUpDb(container); 09 container.ext().configure().clientServer().batchMessages(true); 10 fillUpDb(container); 11 } finally { 12 container.close(); 13 } 14 } finally { 15 db4oServer.close(); 16 } 17 }

You can try different values of NO_OF_OBJECTS constant to see the difference.

If the value of NO_OF_OBJECTS is high (>1,000,000) you may notice that the memory consumption increases a lot. In order to decrease it, try using:

Java: 

container.ext().configure().clientServer().maxBatchQueueSize(size);

Specify the size parameter according to the desirable memory consumption limit.