You are here: Basics Operations & Concepts > Transparent Activation/Persistence > Rollback Strategy

Automatic Rollback

When you rollback a transaction only the state of database affected. The state of the objects in memory isn't touched by the rollback method. Wouldn't it be nice when the objects in memory are also rolled back on a transaction rollback? This can be done by providing a rollback strategy for the transparent persistence. For that you need to pass a RollbackStrategy-implementation to the transparent persistence support. The rollback method will be automatically called once per modified object after a rollback. On the rollback method you can deactivate the modified object. This ensures that the object state is cleared and read again from the database. This is a simple but effective strategy and therefore is implemented in the DeactivatingRollbackStrategy.

EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
configuration.common()
        .add(new TransparentPersistenceSupport(new DeactivatingRollbackStrategy()));
RollbackExample.java: Configure rollback strategy

After adding the rollback strategy objects are rolled back aswell.

Pilot pilot = container.query(Pilot.class).get(0);
pilot.setName("NewName");
// Rollback
container.rollback();
// Now the pilot has the old name again
System.out.println(pilot.getName());
RollbackExample.java: Rollback with rollback strategy

Note that rollback strategies only work for activatable objects.