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