Question

I have the following use case:

I have a number of standard entities in my application. When a JPA save/merge of an instance of one of these entities is performed by my application, I would like another save to be done of an instance of a special Event entity. Thus, for each operation on an standard entity, an event is inserted into the database.

I am considering using AOP to implement this cross-cutting concern. However, what we want to avoid at all cost is for a standard entity to be created and no event being created or vice-versa: a rollback occuring on the insert of the standard entity and the event being created.

How then can I ensure atomicity of my operation with Spring AOP?

Any clue or tip welcome.

Was it helpful?

Solution

Just make sure the aspect to create the event runs in the same transaction as the transaction that creates the standard entity. One way to do this is to make sure the order of the transaction aspect puts it outside your event save aspect.

EDIT: According to http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html#transaction-declarative-annotations it is quite simple to ensure the Transactional aspect is applied before any other aspect.

Include an order attribute in your tx definition...

<tx:annotation-driven order="-1"/>

Thus as long as your other aspects have an order greater than -1 the transactional aspect will run first. See http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/aop.html#aop-ataspectj-advice-ordering for more info on aspect ordering.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top