Transactions and Concurrency

Transaction Isolation

The ElmoManager is in auto-commit mode by default with no active transaction. To activate a transaction call the begin method on the transaction object returned from getTransaction - manager.getTransaction().begin(). The transaction is closed by calling manager.getTransaction().commit() or manager.getTransaction().rollback(). It is recommended to use only one active transaction per manager.

Undo Support

ElmoManager implements the memento design pattern. Because Elmo is transaction based, only operations that were performed by the entity pool the memento was created in, will be undone. When using multiple entity pools, or using the ThreadProxyRepository with multiple connections, multiple memento objects must be created for each. Memento support is only available when the repository stack contains a notifying repository. Below is an example of how to use the undo feature in Elmo.

ElmoModule module = new ElmoModule();
module.addRole(Employee.class);
repository = new SailRepository(new MemoryStore());
repository = new NotifyingRepositoryWrapper(repository, true);
repository.initialize();
factory = new SesameManagerFactory(module, repository);
manager = factory.createElmoManager();

Employee emp = manager.designate(Employee.class);
Memento memento = manager.createMemento();
emp.setName("John");
assertEquals("John", emp.getName());

manager.undoMemento(memento);
assertNull(emp.getName());

The memento object does not lock any modified resources. If the same resources have been modified outside of the memento object and cause a conflict when the memento is undone, unexpected results can appear in the repository, so use with caution.