开发者

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 t开发者_高级运维o 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?


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜