Should web services be transactional?

巧了我就是萌 提交于 2019-12-22 10:48:48

问题


I am investigating writing web services for an application. Within this application we do everything within transactions as "units of work" are often not single entities but multiple entities spanning multiple tables. There are situations where we want "All or nothing" and a transaction makes perfect sense. I am not really sure how to do this in a web-service however, nor if I even should.

I feel like web services should be stateless and the provided API should be built on a per-entity basis, yet I am unsure how to handle the "units of work" that should one part fail, a rollback should occur.

Should Web services be transactional? How would you implement transactions, would it be something along the lines of sending a "BEGIN TRANSACTION" and ending with an "END TRANSACTION"?

If web-services are stateless how do you deal with "units of work" that are not independent? Is there any definitive literature around that I can read on the topic?

thanks,


回答1:


The web service might be all or none behind the scenes, but you have a choice as to what you expose to the client.

If you want the client to know about commit/rollback behavior, you have to provide "compensating transactions" as part of your API. So if one use case creates a new transaction, you have to provide a complementary method to delete it and tell the user if the create succeeded or failed. Same with all other methods.

I think this exposes too much of what the service is doing. Clients are forced to know too much. It's a poor encapsulation.

I think it's a better design to leave that knowledge hidden inside the service. Report back to the user about success or failure, but that's it.

This means you have to design your service to be coarse-grained enough to encompass a complete unit of work. Let it orchestrate data sources and other services to fulfill the use case.




回答2:


One of the canonical papers on the subject is Pat Helland's Life Beyond Distributed Transactions: an Apostate's Opinion. Highly recommended.



来源:https://stackoverflow.com/questions/6668126/should-web-services-be-transactional

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!