Search code examples
web-servicesrestwsdl

Should web services be transactional?


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,


Solution

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