简体   繁体   中英

Create a transaction like to use EJB container administration

I have a code in my business layer that updates data on database and also in a rest service.

The question is that if it doesn't fail data must be save in both places and, in other hand, if it fails it must to rollback in database and send another requisition to rest api.

So, what I'm looking for is a way to use transaction management of EJB to also orchestrait calls to api. When in commit time, send a set requisition to api and, when in rollback time, send delete requisition to api.

In fact I need to maintain consistency and make both places syncronous.

I have read about UserTransactions and managedbeans but I don't have a clue about what is the best way to do that.

You can use regular distributed transactions, depending on your infrastructure and participants. This might be possible eg if all participants are EJBs and the data stores are capable to handle distributed transactions.

This won't work with loosely coupled componentes, and your setup looks like this.

I do not recommend to create your own distributed transaction protocol. Regarding the edge and corner cases, you will probably not end up with consistent data in the end.

I would suggest to think about using event sourcing and eventually consistency for things like that. For example, you could emit an event (command) for writing data. If your "rollback" is needed, you can emit an event (command) to delete the date written before. After all events are processed, the data is consistent.

Some interesting links might be:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM