Dachs - Data Change Snitch

A unified entity change-listener across different persistence APIs and implementations.
Rationale
In almost all back-end systems there is the need to notify or update additional data whenever core data changes. It can be sending a notification event, invalidating cache entries, audit logging or updating a search index to mention a few.
For example Spring has already support for events, and from Spring 4.2 it also has support for send-at-end-of-transaction-events. Further Spring JPA supports auditing, however it does not support fetching the actual data, just who changed it and when. There is no recollection of what was changed.

All the different persistence frameworks have their different APIs for detecting data changes. With Dachs you have one simple, unified API to deal with.
Maven artifact
<dependency>
<groupId>com.ethlo.dachs</groupId>
<artifactId>dachs-{impl}</artifactId>
<version>1.4.0</version>
</dependency>
Supported persistence frameworks
Compatibility
|
Supported and tested |
| Java |
17, 21, 25 |
| Spring Boot |
4.0.x |
| Hibernate ORM |
7.x, as managed by Spring Boot (tested with 7.2) |
| EclipseLink |
5.0.x (tested with 5.0.2) |
| Transaction manager |
NotifyingJpaTransactionManager (a JpaTransactionManager), with transaction synchronization enabled (the default). JTA is not supported. |
See the changelog for changes between releases.
API
The goal is to have a simple, but powerful API to get notifications of all changes to entities, that is created, updated and deleted.
Transactional listener
This listener will buffer all events until the transaction is about to commit. preDataChanged is called just before the transaction is committed. Any exception here will abort the transaction. postDataChanged is called just after the transaction is committed.
public interface EntityChangeSetListener
{
void preDataChanged(EntityDataChangeSet changeset);
void postDataChanged(EntityDataChangeSet changeset);
}
Both methods gives you an EntityDataChangeSet which holds all operations performed inside the transaction.
public interface EntityDataChangeSet
{
Collection<EntityDataChange> getCreated();
Collection<EntityDataChange> getUpdated();
Collection<EntityDataChange> getDeleted();
boolean isEmpty();
}
Transaction semantics
- Scoped to the transaction that made the changes. A change set only holds changes made in the transaction being committed. With
PROPAGATION_REQUIRES_NEW, the inner transaction publishes only its own changes, and the outer transaction publishes its changes at its own commit. Nothing is published for a transaction that rolls back. If the commit itself fails, EntityChangeListener’s rolledBack* callbacks receive every change the transaction made, including those already passed to preDataChanged.
- Only transactions run by
NotifyingJpaTransactionManager are published. The Dachs auto-configuration installs it as the transaction manager. Changes made in a transaction run by any other transaction manager (a second JpaTransactionManager, JTA, a manually managed EntityTransaction) are not published to EntityChangeSetListeners, and are discarded when that transaction ends. EntityChangeListener still receives them.
- Exceptions after commit do not reach the caller. By the time
postDataChanged, TransactionListener or the rolledBack* callbacks run, the outcome of the transaction is final. An exception from one of them is logged at ERROR level by com.ethlo.dachs.jpa.DefaultInternalEntityListener, and the remaining listeners are still called. An exception from preDataChanged still aborts the transaction.
- Change sets cannot be modified. Every collection in a change set, and each change’s property changes, throws
UnsupportedOperationException on modification, so one listener cannot affect what other listeners receive. To pass on a reduced change set, build a new one with EntityDataChangeSet.of(created, updated, deleted), or use DefaultFilteredEntityChangeSetListener.
- One update per entity. If an entity is flushed several times in a transaction, the change set holds one update for it: each property has the old value from before the transaction’s first change and the new value from its last.
- Only persistent state. Static,
transient and @Transient fields, and fields annotated with @EntityListenerIgnore, are not reported. Changes made by preDataChanged listeners themselves are only reported to postDataChanged.
- Delivery is in-process and best effort. If the application stops between the commit and
postDataChanged, the event is lost. If you must never lose an event (audit, outbox, search indexing), write it to the database in preDataChanged, as part of the same transaction.
Non-transactional listener
EntityChangeListener
public interface EntityChangeListener
{
void preCreate(EntityDataChange entityData);
void preUpdate(EntityDataChange entityData);
void preDelete(EntityDataChange entityData);
void created(EntityDataChange entityData);
void updated(EntityDataChange entityData);
void deleted(EntityDataChange entityData);
}
Using this simple listener, we get an EntityDataChange object for each operation on the entity.
Common for both trasactional and non-transactional
public interface EntityDataChange
{
Serializable getId();
Object getEntity();
Collection<PropertyChange<?>> getPropertyChanges();
Optional<PropertyChange<?>> getPropertyChange(String propertyName);
}
Each EntityDataChange object holds a collection of PropertyChanges that is the individual properties that has changed.
public interface PropertyChange<T>
{
String getPropertyName();
Class<T> getPropertyType();
T getOldValue();
T getNewValue();
}
Example output
Given a simple Person object:
public class Person()
{
private String name;
private Integer age;
}
Created
EntityData
propertyChanges:
* name - null => "John Doe"
* age - null => 34
Updated
EntityData
propertyChanges:
* name - "John Doe" => "John Smith"
* age - 34 => 47
Deleted
EntityData
propertyChanges:
* name - "John Smith" => null
* age - 47 => null
Limitations
Dachs relies on the persistence framework in use to notify about operations and there might be limitations.
In general bulk delete will not trigger delete events (aka DELETE FROM Entity).
评论