Transactions in webservices

L

Lars Siden

Hi,

Yesterday I made a test on using transactions in a
webservice. I created a test database containing one
table ( ID PK, Data varchar2 ).

Code snippet:
-------------
[WebMethod
(TransactionOption=TransactionOption.RequiresNew)]
public DataSet Test_Transaction(int iID, string sText)
{
try
{
DataSet ds = new DataSet();
// Insert a row into DB
ds = Add_Data(iID+1,"Working:" + sText);
// Will fail due to ID already exists
ds = Add_Data(iID,sText);
return ds;
}
catch
{
ContextUtil.SetAbort();
DataSet ds = new DataSet();
// This dataset contains rollbacked data?!?!
dbAdap1.Fill( ds,"Test");
return ds;
}
}
// Add method
protected DataSet Add_Data ( int iID, string sText )
{
try
{
DataSet ds = new DataSet();
dbAdap.Fill(ds,"Test");
DataRow dr = ds.Tables["Test"].NewRow();
dr["ID"] = iID;
dr["Data"] = sText;
ds.Tables["Test"].Rows.Add(dr);
dbAdap.Update(ds,"Test");
return ds;
}
catch ( Exception e )
{
ContextUtil.SetAbort();
throw e;
}
}

// End Code ---------------------

1)
When I use the above code everyhing do work when the call
to the webmethod is finished - That is, no rows are added
to the DB if an exception is thrown. The strange thing is
that in the catch statement in the webmethod, if I do a
new "fill", I get the row that should/would be
rollbacked? why is that? If I do another function call
after the webmethod has returned, I get the correct data.

2)
I've understood that using the TransactionOption on the
webmethod attribute is quite limited, so I wonder, would
it be better to make a servicedcomponent and use that
component from within my webservice? Then the
servicedcomponent would do all transaction handling and
the webservices should only serve as an interface to the
client?

3)
Where can I learn more about this? I've searched MSDN and
the newsgroup finding where few examples, almost none at
all about transactions in webservices.

Best regards,

Lars Siden

Datarutin AB
 
M

MSFT

Hi Lars,

"ContextUtil.SetAbort();" only will set the consistent bit to false and the
done bit to true in the COM+ context. It won't cause the rollback
immediately in the database, therefore, you may still get the incorrect
data. To correct this problem, you may change your code to:

DataSet ds1 = new DataSet();
dbAdap1.Fill(ds1,"Table4");

DataSet ds = new DataSet1();

try
{


ds = Add_Data(iID+1,"Working:" + sText);

ds = Add_Data(iID,sText);
return ds;
}
catch
{
ContextUtil.SetAbort();

return ds1;
}

For more inforamtion on this, you may refer to:

Voting in an Automatic Transaction
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconvotinginautomatictransaction.asp

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top