Migrating Between Databases

Transparent activation and persistence functionality depends on an association between an object and an object container, which is created when an activator is bound to the object. Each object allows only one activator. Typically this limitation won't show up, however there is a valid use case for it:

1)      suppose you need to copy one or more objects from one object container to another;

2)      you will retrieve the object(s) from the first object container using any suitable query syntax;

3)      optionally you can close the first object container;

4)      you will now save the object to the second object container.

If both object containers were using transparent activation or persistence - the 4-th step will throw an exception. Let's look at the case in more detail. Typical activatable class contains an activator field. When transparent activation functionality is used for the first time an object container activator will be bound to the object:

SensorPanelTA.java: bind
01/*Bind the class to the specified object container, create the activator*/ 02 public void bind(Activator activator) { 03 if (_activator == activator) { 04 return; 05 } 06 if (activator != null && _activator != null) { 07 throw new IllegalStateException(); 08 } 09 _activator = activator; 10 }

If bind method will be re-called with the same object container, activator parameter will always be the same. However, if another object container tries to bind the object (in our case with the store call) activator parameter will be different, which will cause an exception. (Exception will be thrown even if the first object container is already closed, as activator object still exists in the memory.) This behaviour is illustrated with the following example (SensorPanelTA class from Transparent Activation chapter is used):

TAExample.java: testSwitchDatabases
01private static void testSwitchDatabases() { 02 storeSensorPanel(); 03 04 ObjectContainer firstDb = Db4o.openFile(configureTA(), FIRST_DB_NAME); 05 ObjectContainer secondDb = Db4o.openFile(configureTA(), SECOND_DB_NAME); 06 try { 07 ObjectSet result = firstDb.queryByExample(new SensorPanelTA(1)); 08 if (result.size() > 0) { 09 SensorPanelTA sensor = (SensorPanelTA) result.get(0); 10 firstDb.close(); 11 // Migrating an object from the first database 12 // into a second database 13 secondDb.store(sensor); 14 } 15 } finally { 16 firstDb.close(); 17 secondDb.close(); 18 } 19 }

The solution to this problem is simple: activator should be unbound from the object:

Java:

sensor.bind(null);

Note, that the object will quit being activatable for the first object container. The following example shows the described behaviour:

TAExample.java: testSwitchDatabasesFixed
01private static void testSwitchDatabasesFixed() { 02 storeSensorPanel(); 03 04 ObjectContainer firstDb = Db4o.openFile(configureTA(), FIRST_DB_NAME); 05 ObjectContainer secondDb = Db4o.openFile(configureTA(), SECOND_DB_NAME); 06 try { 07 ObjectSet result = firstDb.queryByExample(new SensorPanelTA(1)); 08 if (result.size() > 0) { 09 SensorPanelTA sensor = (SensorPanelTA) result.get(0); 10 // Unbind the object from the first database 11 sensor.bind(null); 12 // Migrating the object into the second database 13 secondDb.store(sensor); 14 15 16 System.out.println("Retrieving previous query results from " 17 + FIRST_DB_NAME + ":"); 18 SensorPanelTA next = sensor.getNext(); 19 while (next != null) { 20 System.out.println(next); 21 next = next.getNext(); 22 } 23 24 System.out.println("Retrieving previous query results from " 25 + FIRST_DB_NAME + " with manual activation:"); 26 firstDb.activate(sensor, Integer.MAX_VALUE); 27 next = sensor.getNext(); 28 while (next != null) { 29 System.out.println(next); 30 next = next.getNext(); 31 } 32 33 System.out.println("Retrieving sensorPanel from " + SECOND_DB_NAME + ":"); 34 result = secondDb.queryByExample(new SensorPanelTA(1)); 35 next = sensor.getNext(); 36 while (next != null) { 37 System.out.println(next); 38 next = next.getNext(); 39 } 40 } 41 } finally { 42 firstDb.close(); 43 secondDb.close(); 44 } 45 }