John Engelman
John Engelman

Reputation: 4429

Have an external java application by notified of changes to Entity EJBs in JBoss AS

I'm trying to connect an external application to a JBoss AS container. The external application is a Java application that is currently being notified of changes to database entities through a JMS topic. I've added an EntityLifecycleListener class to all my entities that publishes a serialized (and unwrapped) copy of the entity to the JMS topic.

The problem is that this implementation ignores the transaction boundaries of the JBoss container. For example, the @PostUpdate event can be fire, generating the JMS message for that entity, but the transaction could be rolled back causing the external application to be notified of an invalid change and become unsync'd.

I need my external application to only be notified of successful commits to the database, but I need to be able to publish the entire java POJO to the external application. Is there an official way of doing this?

Upvotes: 1

Views: 354

Answers (1)

ewernli
ewernli

Reputation: 38625

The JPA spec are a bit vague about transaction demarcation and the listener (§ 3.5.2):

The PreUpdate and PostUpdate callbacks occur before and after the database update operations to entity data respectively.

I had a similar situation, and so far I remember, the callback @PostXxxx were sometimes executed in the correct transaction or no transaction at all. That depends whether the update was an "intermediate" flush in the transaction or the "last" flush before the transaction is committed, in which case the callback happens after the transaction was committed (which makes sense).

I suggest you try with @PreXxxxx which should always be in the transaction. (Also, if I remember well, the only problem with @PreXxxx was that the entity may not be assigned a PK yet, whether it's problematic depends on your use case.)

Upvotes: 1

Related Questions