Persistence

maxdepth:3

Definition

Root for all persistence related components. Based on Hibernate, provides several persistence related behaviours in a transparent way. Persistence is fundamental for many features in a mission critical system with strong security concerns, due to tracking and auditing needs. Since almost no operations should be allowed without a persistent record, the preferred way of implementing actions are through persistence side effects. Since mission critical transactional systems have a intrinsic need for data integrity and distributed transactions, the persistence layer is build upon SQL databases. Most of RDBMS integration is made through Hibernate, allowing for code suitable for most relational databases.

Listeners

Given the intrinsic need for persistence side effects, there must be a clear way to express them. Jmine provides an interface called PersisterListener, for objects registered appropriately that should be warned on entity-related persistence events. An easy way to set up persister listeners for a sample module is like the following:

  1. Implement an action that should be executed at some point:
public class MySideEffect implements Action<MyEntity> {
        private SomeDependency dependency;
        public MySideEffect(@Injected SomeDependency dependency) {
                this.dependency = dependency;
        }
        public void act(MyEntity target) {
                ...
        }
}
  1. Wire it to some persistence event:
public void MyPersisterListenerConfigurer implements PersisterListenerConfigurer {
        public void configure(PersisterListeners listeners) {
                listeners.afterInsert(MyEntity.class, MySideEffect.class);
        }
}
  1. Add your configurer to spring’s bean factory:
<xml>
        <bean id="myPersisterListenerConfigurer" class="org.my.MyPersisterListenerConfigurer"/>
</xml>
  1. Thats it!

A few hints:

  • A configurer might set up several persister listeners, you may organize them to be as readable as possible.
  • Dependency Injection is provided to persistence actions, just annotate constructor dependencies or dependency setters with @Injected and it will all be filled prior to execution.
  • Use @Documentation at your action class and get a nice persister listener description, easing later debugging and quickly understanding all side effects related to an entity.
  • Your action has a single responsibility, implement it wisely, use the provided dependency injection, unit test it before you wire it.
  • Side effects may have dependencies between them (although it should be avoided). You can also describe dependencies explicitly, as shown on the example below.
public void MyPersisterListenerConfigurer implements PersisterListenerConfigurer {
        public void configure(PersisterListeners listeners) {
                listeners.afterInsert(MyEntity.class, MySideEffect1.class);
                listeners.afterInsert(MyEntity.class, MySideEffect2.class);
                listeners.action(MySideEffect1.class).dependsOn(MySideEffect2.class);
        }
}