How are transactions managed over multiple webservice requests?

2 minute read time.
This is an interesting question and depending of what we understand by the word 'transactions' it can have a couple of answers.

Answer 1

The first thing to note is that each webservice request is an HTTP transaction. A request is made and the reponse is given, so the question could be about how the session information is maintained over several web service requests.

Consider this code (C#)

[code language="csharp"]
logonresult CRMlogon = CRM60.logon("Admin", "");
CRM60.SessionHeaderValue = new SessionHeader();
CRM60.SessionHeaderValue.sessionId = CRMlogon.sessionid;
string strSessionID = CRMlogon.sessionid;
labelSessionID.Text = strSessionID;
getversionstringresult CRMVersion = CRM60.getversionstring();
[/code]

This is simple enough and it nicely demonstrates the fact that 2 distinct HTTP requests are made. The first is for the logon() (in blue) and the second for the getversionstring() (in red).

In the logs as this is shown as

[code language="xml"]
Request Content : Admin
Web Service API logon being called.
Attempting to login Admin .
User Loggedin.
Response Content :  16623346823897

Request Content : ; 16623346823897 ;
Web Service API sessionheader being called.
Web Service API getversionstring being called.
Response Content :  Trial Version XX Build 43
[/code]

We can see that in the second request produced by the getversionstring() method the session id has to be passed.

Any subsequent webservice request will also pass in the session id until the session is destroyed.

Answer 2

But the question of how transactions are handled could also refer to database interactions. A single webservice request may invoke changes to multiple rows held in many database tables. For example we may want to add in a new company. In CRM a company actually consists of records held in the company table, in the person table, address, phone and email etc. This concept of an entity has been discussed in other articles.

If we look at the code (C#) associated with the addition of a new company entity into the database:

[code language="csharp"]
ewarebase[] CRMBase = new ewarebase[1];
company newCompany = new company();
crmid newRecordID = new crmid();

newCompany.name = "Sage";
newCompany.people= new ewarebaselist();
newCompany.people.records=new ewarebase[1];
newCompany.people.entityname="person";

newCompany.phone = new ewarebaselist();
newCompany.phone.records = new ewarebase[2];
newCompany.phone.entityname = "phone";

person newPerson = new person();
newPerson.firstname="Jeff";
newPerson.lastname="Richards";
newCompany.people.records[0]=newPerson;

phone newPhone1 = new phone();
newPhone1.type = "Business";
newPhone1.countrycode = "44";
newPhone1.areacode = "20";
newPhone1.number = "7701076";
newCompany.phone.records[0] = newPhone1;

phone newPhone2 = new phone();
newPhone2.type = "Fax";
newPhone2.countrycode = "44";
newPhone2.areacode = "20";
newPhone2.number = "7701077";
newCompany.phone.records[1] = newPhone2;

CRMBase[0] = newCompany;
addresult CRMAddResult = CRM60.add("company", CRMBase);
[/code]

We can see that there is one add() method call to insert the entity into the system. That method would pass all the data for each of the seperate tables within the XML message sent by the webservice request. If there was an error then all of this transaction would fail. The database transaction that is associated with the single webservice request then spans the insertion of multiple records.