Sending events at the end of a transaction

前端 未结 3 979
闹比i
闹比i 2021-02-08 06:50

I have an interface for a Service object that looks something like the following (simplified for brevity):

public interface ItemService {

  public Item getItemB         


        
3条回答
  •  故里飘歌
    2021-02-08 07:08

    In contrast to Wouter Coekaerts I understand that you are asking for an way to send notifications that will be only submitted to the reciver if the transaction in which they are created was succesfull. -- So you are looking for something that is similar to the transactional CDI-Event mechanism.

    My idea to solve it is this way:

    • send the events and "store" them in a list in your event handling mechanism, but do not forward the events to the recivers. (I guess that is easy to implement)
    • forward the events from the list to the recviers if the transaction was sucessfull
    • delete the events from the list if the transaction has a roll-backed

    To forward or delete the events I would first have a lookt at the spring transaction mechanism. If there is no way to extend it, you could write an AOP aspect that forward the events if a method annotated with @Transactional was leaving wihtout an (Runtime)Exception. But if the @Transactional annotated Method was leaving with an (Runtime)Exception then delete the events from the list.

提交回复
热议问题